How check if shape vector contains a shape with a certain value - hacklang

shape x {
?'a' => ?string,
?'b' => ?string,
}
I have an array of shape x, And I am trying to see if any of the shapes have a value of 'hello' in field 'a'. How can i do so using built in functions?

function has_hello(vec<shape(?'a' => string, ?'b' => string)> $items): bool {
foreach ($items as $item) {
if (Shapes::idx($item, 'a') === 'hello') {
return true;
}
}
return false;
}
You can use Shapes::idx to get the value of an optional field. If you're looking for helper functions to iterate on arrays, check out the C\ namespace.

Related

Creating code based on value of macro agrument rust

macro_rules! retry_put {
($mod_name:ident, $data_type:ty) => {{
fn $mod_name() {
// somelike
if $mod_name == "red" {
// generate code written here and not the one in else block
return u8;
}
else {
// generate code written here and not the one in if
return "string";
}
}
}
}
I am tring to change the return type based on input basically, if input is true return string else return int.
Or maybe give example for :
give example where we are accepting a arguement in macro and if its even calulate factorial of 5 and return it as integer and if the agruement is odd calculate the factoreial of 5 and return it as string. And name of both functions should be same. and logic of calculating 5! should not be repeated.
You can overload a macro like this:
macro_rules! retry_put {
(red, $data_type:ty) => {{
fn red() {
return u8;
}
}
}
($mod_name:ident, $data_type:ty) => {{
fn $mod_name() {
return "string";
}
}
}
}
See macro_rules!.

how can i return String value from list by it's name value in dart list

How can i handle the following code
List books = ['book1', book2]
String getTextWidget() {
return // here i need to only return the element which is 'book1' such as if books.contains('book1') return this element as String ;
}
the i need to put it in Text Widget like so
Container(
child Text(getTextWidget())
)
i tried the following code , it is work but it does not accept String Widget
getTextWidget() {
books .forEach((element) {
if(element.contains('book1')){
'book1'
}
});
}
I think you'd benefit from the .firstWhere method on your list.
void main() {
// an arbitrary object that is not type String
final book2 = Object();
List books = ['book1', book2];
print(getTextElement(books)); // book1
}
String? getTextElement(List list) {
return list.firstWhere((e) => e is String);
}
try this , give "book1" to the methode as a parameter
String getTextWidget(String nameofbook) {
if(books.contains(nameofbook)==true){
return nameofbook;
}
else return null;
}

How to iterate fields of a shape in hacklang?

Say I have a shape like this
$something = shape(
'some_key' => ...,
'another_key' => ...,
...
);
How can I iterate each field of the shape? I'm looking for something like this
foreach ($something as $key) {
...
}
Convert to a dict first with the built-in HH\Shapes::toDict then iterate:
foreach(HH\Shapes::toDict($something) as $k => $v) {
// ...
}

Get the list of item from list that contains other list

I have a list of string.
I want to get the list items that matches the other list.
i.e, if First List values contains the value from other string return all the values.
List<string> fileNameToMatch = new List<string> { "nsevar", "cat", "elm", "nse isin", "l6scripsv2eq" };
List<string> fileToMatch = new List<string> { "fevnsevarc", "gfcatgf","ratstts","mymatch"};
The second list contains the values the are present in first list.
So return all values.
The output should be "fevnsevarc" and "gfcatgf" in list.
Can we use some link to get the names from the fileToMatch which contain data from fileNameToMatch
This should do the trick:
var res = fileToMatch
.Where(f => fileNameToMatch.Any(fn => f.IndexOf(fn) >= 0))
.ToList();
This is nearly self-explanatory: you are looking for all files f in fileToMatch such that a file name fn exists in the fileNameToMatch where f is contained anywhere in fn as a substring.
EDIT : (in response to a comment) To get the fn along with f, add a Select, and use an anonymous type, like this:
var res = fileToMatch
.Where(f => fileNameToMatch.Any(fn => f.IndexOf(fn) >= 0))
.Select(f => new {
File = f
, Name = fileNameToMatch.First(fn => f.IndexOf(fn) >= 0)
})
.ToList();
foreach (var match in res) {
Console.WriteLine("{0} - {1}", match.File, match.Name);
}
This should work:
var result = fileToMatch.Where(x => fileNameToMatch.Any(y => x.Contains(y)));

Groovy equivalent to OCL forAll

What is the Groovy equivalent to the forAll method in OCL?
Let's say that I have a list of items.
def items = new LinkedList<Item>();
What is the Groovy way to express a predicate that holds if and only if all items match a certain criteria?
The following code snippet does not work, because the inner return only jumps out of the current iteration of the each closure, not out of the forAll method.
boolean forAll(def items)
{
items.each { item -> if (!item.matchesCriteria()) return false; };
return true;
}
The following code snippet, which should do the trick, feels cumbersome and not Groovy-like.
boolean forAll(def items)
{
boolean acceptable = true;
items.each { item -> if (!item.matchesCriteria()) acceptable = false; };
return acceptable;
}
I am looking for a way to lazily evaluate the predicate, so that the evaluation would finish when a first non-matching item is found.
You can use every
items.every { it.matchesCriteria() }
In groovy that's very easy:
def yourCollection = [0,1,"", "sunshine", true,false]
assert yourCollection.any() // If any element is true
or if you want to make sure, all are true
assert !yourCollection.every()
you can even do it with a closure
assert yourCollection.any { it == "sunshine" } // matches one element, and returns true
or
assert !yourCollection.every { it == "sunshine" } // does not match all elements

Resources