Eachable1 type

Eachable which each() calls the callback with one argument

Direct parent types

Eachable
Parent type of all types that have each() method
Direct subtypes: 2

Direct children types

Arr
Array - ordered list of items accessed by zero-based index
ArrIter
Arr (array) iterator.
ArrLike
Parent type for user-defined types with array-like behaviour. Use in cases when you would like to inherit from built-in Arr. Inheriting from built-ins is not possible for now.
Direct subtypes: 1
Box
A box which might (FullBox) or might not (EmptyBox) contain a value. Box is somewhat similar to Arr of zero (EmptyBox) or exactly one (FullBox) element. This analogy helps understanding each() and filter() operation.
Direct subtypes: 2
ConstIter
Iterator that returns given value forever.
FilterIter
EXPERIMENTAL! Do not use!
FunIter
EXPERIMENTAL! Do not use!
Int
Integer type. On 64 bit platforms it's a 61 bit signed integer
MapIter
EXPERIMENTAL! Do not use!
MultiMethod
MultiMethod, container for methods.
NumRange
Numerical range
RangeIter
Iterates over a Range
ResDef
Abstract resource definition
Direct subtypes: 2
Set
Represents a set of items
Str
String type. Contains sequential bytes.
Table
Holds tabular data

Methods

any(e:Eachable1, predicate:Any) Source: stdlib.ngs:1546
Check whether there is any element in e that satisfies the given predicate.

Parameters

eThe items to check
predicateTest function

Returns

Bool

Example

[1,2,10].any(F(elt) elt > 5)   # true
[1,2,10].any(F(elt) elt > 15)  # false
Arr(something:Eachable1) Source: stdlib.ngs:1886
Convert Eachable1 (anything with "each" method that takes callback of one parameter) to an array

Example

Arr(1..3)   # [1,2]
Arr(1...3)  # [1,2,3]
count(e:Eachable1, predicate:Any) Source: stdlib.ngs:1725
Count number of items that satisfy the predicate.

Parameters

eItems to look at
predicateTest function

Returns

Int

Example

[1,2,3,11,12].count(X>10)  # 2
dflt(e:Eachable1, k:Any, v:Any) Source: stdlib.ngs:960
Set a field on all elements if it's not already set

Parameters

eEachable1 with elements of type NormalTypeInstance or Hash

Returns

e

Example

my_items.dflt("source", "(unknown)")
drop(e:Eachable1, predicate:Any)experimental Source: stdlib.ngs:484
EXPERIMENTAL! Do not use! Filters out items that satisfy predicate at the begginning of e.

Example

[1,2,3,1,2].drop(X<3)  # [3,1,2]
each_chunk(e:Eachable1, n:Int, cb:Fun)experimental Source: stdlib.ngs:2199
Call cb with array of maximum length of n, repeatedely for all items of e. TODO: better doc
each_idx_val(e:Eachable1, cb:Fun) Source: stdlib.ngs:1619
Iterates over elements of e, passing each in turn to cb along with index and args: cb(INDEX, ITEM)

Returns

e

Example

"abc".each_idx_val(F(idx, val) echo("Element #$idx equals to $val"))
filter(e:Eachable1, predicate:Any) Source: stdlib.ngs:302
Filter e using predicate.

Parameters

eEachable1. WARNING: instances of same type as e must have empty constructor and push() method.
predicateDecision function to be called with each item as first argument. When predicate(item) returns true, the item will appear in the resulting array.

Returns

Of same type as e

Example

(1...10).filter(F(num) num % 2 == 0)  # Gives [2,4,6,8,10]
(1...10).filter(X<5)  # Gives [1,2,3,4], predicate called as predicate(item, 5)
filter(something:Eachable1, field:Str, predicate:Any)deprecated Source: stdlib.ngs:317
DEPRECATED! Do not use! Use something.filter({field: predicate}) instead.
filter(e:Eachable1)deprecated Source: stdlib.ngs:442
Filter nulls out. DEPRECATED, USE something.without(null) INSTEAD.

Returns

Arr (array) of original items without nulls
first(e:Eachable1, predicate:Any, default:Any) Source: stdlib.ngs:346
Find first element in e that satisfies the predicate.

Returns

Either the element or the provided default if element was not found.

Example

(10..20).first(F(x) x % 3 == 0)        # 12 - first item divisible by 3
(10..20).first(F(x) x % 50 == 0, 1000) # 1000
first(e:Eachable1, predicate:Any) Source: stdlib.ngs:362
Find first element in e that satisfies the predicate. Throws ElementNotFound exception if no such element is found.

Returns

The element.

Example

(10..20).first(F(x) x % 3 == 0)  # 12 - first item divisible by 3
(10..20).first(F(x) x % 50 == 0) # ElementNotFound exception
first(e:Eachable1) Source: stdlib.ngs:377
Find first element in e that satisfies Bool(e) == true. Exactly same as first(e, identity).

Example

[false, 0, null, "xyz", 100].first()  # "xyz"
[].first()                            # ElementNotFound exception
flatten(e:Eachable1) Source: stdlib.ngs:1748
Flatten one level.

Parameters

eEachable with each element also Eachable

Returns

Arr

Example

[[1], [2,3]].flatten()  # [1,2,3]
get(e:Eachable1, field:Str) Source: stdlib.ngs:1470
Return array made of given field of each element of given Eachable1 where present

Returns

Arr

Example

[{"x": 1}, {"y": 2}].get("x")  # [1]
``aws ec2 describe-instances``.Tags.get("role").uniq()
# Returns Arr of Str with roles. Does not crash if some machines do not have "role" tag.
get(e:Eachable1, field:Str, dflt:Any) Source: stdlib.ngs:1478
Return array made of given field of each element of given Eachable1 where present or default value where the field is not present.

Returns

Arr

Example

[{"x": 1}, {"y": 2}].get("x", null)  # [1, null]
group(e:Eachable1, cb:Fun) Source: stdlib.ngs:2593
Group items from e by key returned by cb

Returns

Hash with Arr values

Example

["ab", "ac", "ba", "bc", "bd"].group(F(x) x[0])  # {a=[ab,ac], b=[ba,bc,bd]}
Hash(e:Eachable1, key_field:Str, val_field:Str) Source: stdlib.ngs:2535
Create Hash from Arr of something that has key and value fields

Parameters

eEachable1 with all the keys and values
key_fieldName of the field holding the keys of newly created Hash
val_fieldName of the field holding the values of newly created Hash

Returns

Hash

Example

Hash([{"Name": "n1", "Value": "v1"},{"Name": "n2", "Value": "v2"}], "Name", "Value")  # {"n1": "v1", "n2": "v2"}
in(x:Any, e:Eachable1) Source: stdlib.ngs:610
Checks whether element x is in Eachable1

Parameters

xNeedle
eHaystack

Returns

Bool

Example

 1 in [1,2,3].Iter()  # true
10 in [1,2,3].Iter()  # false
init(s:Set, e:Eachable1) Source: autoload/Set.ngs:12
Convert array to set
map_idx_val(e:Eachable1, mapper:Fun) Source: stdlib.ngs:1641
Map an Eachable1 to an Arr (array) of values using mapper mapper is called as mapper(INDEX, ITEM)

Returns

New Arr

Example

echo("Array items: " + ArrLike().push(10).push(20).push(30).map_idx_val(F(idx, val) "[$idx]=$val").join(", "))
# Outputs: Array items: [0]=10, [1]=20, [2]=30
mapo(e:Eachable1, mapper:Fun)experimental Source: stdlib.ngs:268
EXPERIMENTAL! Do not use! Map e to same type. Mnemonics: "map original" / "MAP to Original type".

Parameters

eobject of any type that has each(e, callback) implemented Eachable1. WARNING: instances of same type as e must have empty constructor and push() method.

Returns

Of same type as e

Example

Set([1,2]).mapo(X*2)  # Set([2,4])
max(e:Eachable1) Source: stdlib.ngs:2043
Find maximal element under (>)
max(e:Eachable1, cb:Fun)deprecated Source: stdlib.ngs:2060
EXPERIMENTAL! Do not use!
min(e:Eachable1) Source: stdlib.ngs:2041
Find minimnal element under (<)
min(e:Eachable1, cb:Fun)deprecated Source: stdlib.ngs:2054
EXPERIMENTAL! Do not use!
none(e:Eachable1, predicate:Fun) Source: stdlib.ngs:1581
Check that there is no element in e that satisfies the given predicate. Exactly same as not(any(e, predicate)) .

Returns

Bool

Example

[0,1,2].none(X>2)  # true
[0,1,2].none(X<2)  # false
partition(something:Eachable1, predicate:Any) Source: stdlib.ngs:328
Partition elements in something according to the predicate into two arrays. First array contains all elements for which predicate(elt) is true and the second array contains all elements for which predicate(elt) is false.

Parameters

somethingEachable1

Returns

Array with two arrays in it

Example

partition([1,10,2,20], X>=10)  # [[10, 20], [1, 2]]
pmap(e:Eachable1, mapper:Fun) Source: stdlib.ngs:3705
Parallel map. Runs mapper in threads. Each thread processes one element from the array but this might change in future (preserving the total number of calls to mapper and the order of results).

Returns

Arr, result of applying mapper to elements of e, preserving the order.

Example

pages_texts = abs_pages_urls.pmap(F(url)  `lynx -dump $url`)
push_all(dst:Any, e:Eachable1) Source: stdlib.ngs:618
Push all elements of e into dst.

Returns

dst
reduce(something:Eachable1, start:Any, f:Fun) Source: stdlib.ngs:516
Combine items to a single value using the supplied binary function First f is applied to start and the first element of something then on each step f is applied to previous result and next element of something.

Parameters

somethingobject of any type that has each(something, callback) implemented
startFirst argument of f, for the first call of f
fThe combining function

Example

F sum(something) something.reduce(0, (+))
reduce(e:Eachable1, f:Fun) Source: stdlib.ngs:537
Combine items to a single value using the supplied binary function First f is applied to the first two elements of e then on each step f is applied to previous result and next element of e. Throws EmptyEachableFail if e has no elements. If e has only one element, that element is returned.

Parameters

eobject of any type that has each(e, callback) implemented
fThe combining function

Example

F sum(e) e.reduce((+))
replace(something:Eachable1, src:Any, dst:Any) Source: stdlib.ngs:503
Replace all occurrences of src with dst TODO: Return same type, not Arr

Parameters

somethingobject of any type that has each(something, callback) implemented

Returns

Arr

Example

["ssh", "IP", "w"].replace("IP", "10.0.0.100")  # ['ssh','10.0.0.100','w']
split(e:Eachable1, delim:Any) Source: stdlib.ngs:2179
Split e into arrays

Returns

Arr of Arr

Example

[1, "a", 2, 3, "a", 4].split("a")  # [[1], [2, 3], [4]]
sum(something:Eachable1) Source: stdlib.ngs:1829
Calculate sum of the elements

Example

[1,2,3].sum()  # 6
take(something:Eachable1, predicate:Any)experimental Source: stdlib.ngs:465
EXPERIMENTAL! Do not use!

Example

[1,2,3,1,2].take(X<3)  # [1,2]
the_one(e:Eachable1, predicate:Any) Source: stdlib.ngs:383
Find the only element that satisfies the predicate. Throws ElementNotFound exception if there are no elements that satisfy the predicate or if there is more than one element that satisfies the predicate.

Returns

The only element that satisfies the predicate.
the_one(e:Eachable1, predicate:Any, body:Fun, found_more:Fun=method, found_none:Fun=method) Source: stdlib.ngs:408
Find the only element that satisfies the predicate and execute given code with the value

Parameters

bodyThe code to execute when exactly one element that satisfies the predicate was found. Executed with the found value. It's value will be returned as result of the_one().
found_moreThe code to execute when more than one element satisfies the predicate. It's value will be returned as result of the_one(). Defaults to function returning null.
found_noneThe code to execute when none of the elements satisfy the predicate. It's value will be returned as result of the_one(). Defaults to function returning null.

Returns

Result of running on of the following: body, found_more, found_none

Example

F name(dn:MethodDocNode) {
	dn.children.the_one(MethodNameDocNode).text[0]
}
the_one(e:Eachable1) Source: stdlib.ngs:434
Extract the only element. Throws ElementNotFound exception if there are no elements or if there is more than one element.

Returns

The only element.

Example

``aws ec2 describe-security-groups --group-ids $sg_id``.the_one()  # {OwnerId=..., VpcId=..., GroupId=..., ...}
without(e:Eachable1, without_elt:Any) Source: stdlib.ngs:1699
Filter out all occurrences of specific value

Parameters

eitems to filter
without_eltThe value to filter out

Returns

Arr

Example

[1,2,3,2].without(2)  # [1,3]