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: 6
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.
Dir
Represents a directory in a file system (S_IFDIR)
Direct subtypes: 1
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
RecIter
Recursively iterates x. Depth-first-search (DFS), pre-order (NLR). See https://en.wikipedia.org/wiki/Tree_traversal
ResDef
Abstract resource definition
Direct subtypes: 2
Set
Represents a set of items
Str
String type. Contains sequential bytes.
Table
Holds tabular data

Methods

+(a:Eachable1, b:Eachable1) Source: stdlib.ngs:1967
Undocumented

Returns

a and b concatenated
+(s:Str, a:Eachable1) Source: stdlib.ngs:5384
Prepend each line in a with s

Returns

Type of ret is same as type of a

Example

"a " + ["1", "2"]  # ["a 1", "a 2"]
+(a:Eachable1, s:Str) Source: stdlib.ngs:5396
Append s to each line in a

Returns

Type of ret is same as type of a

Example

["1", "2"] + " a"  # ["1 a", "2 a"]
.=(a:Arr, field:Str, e:Eachable1) Source: stdlib.ngs:3115
Set field of every element in array to corresponding item in e. Uses Iter(e) internally.

Returns

a

Example

a=[{"x": 1}, {"x": 2}]
a.y = [10, 20]
# a == [{"x": 1, "y": 10}, {"x": 2, "y": 20}]
=~(x:Eachable1, r:Repeat, mc:MatchContext) Source: stdlib.ngs:1461
Check that x is a repetition of r.pattern and it occurs r.count times

Returns

Bool
all(e:Eachable1, pattern:Any=method) Source: stdlib.ngs:3245
Check whether all elements in arr satisfy the given pattern.

Parameters

eThe items to check
patternTest function or anything else acceptable by (=~), defaults to Bool.constructors

Returns

Bool

Example

[1,2,3].all(X<10)   # true
[1,2,10].all(X>5)   # false
any(e:Eachable1, pattern:Any=method) Source: stdlib.ngs:3226
Check whether there is any element in e that satisfies the given pattern.

Parameters

eThe items to check
patternTest function or anything else acceptable by (=~), defaults to Bool.constructors

Returns

Bool

Example

[1,2,10].any(F(elt) elt > 5)   # true
[1,2,10].any(F(elt) elt > 15)  # false
apply(x:Eachable1, f:Fun) Source: stdlib.ngs:817
Applies f to arguments in x Same as calling f(*x)
Arr(something:Eachable1) Source: stdlib.ngs:3571
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]
avg(e:Eachable1) Source: stdlib.ngs:9630
Average

Returns

Real
count(e:Eachable1, pattern:Any=method) Source: stdlib.ngs:3392
Count number of items that match the pattern.

Parameters

eItems to look at
patternTest function or anything else acceptable by (=~), defaults to Bool.constructors

Returns

Int

Example

[1,2,3,11,12].count(X>10)  # 2
deep_copy(e:Eachable1) Source: stdlib.ngs:2010
Returns a copy of the same type, with elements recursively deep-copied
dflt(e:Eachable1, k:Any, v:Any) Source: stdlib.ngs:2737
Set a field on all elements if it's not already set

Parameters

eEachable1

Returns

e

Example

my_items.dflt("source", "(unknown)")
drop(e:Eachable1, pattern:Any)experimental Source: stdlib.ngs:1810
EXPERIMENTAL! Do not use! Filters out items that match the pattern at the beginning of e.

Example

[1,2,3,1,2].drop(X<3)  # [3,1,2]
duplicates(e:Eachable1, field_or_callback:Any) Source: stdlib.ngs:4407
Group the items from e and return groups of more than one element

Returns

Arr of Arr
each_chunk(e:Eachable1, n:Int, cb:Fun)experimental Source: stdlib.ngs:3895
Call cb with array of maximum length of n, repeatedly for all items of e. TODO: better doc, rename to each() to be consistent with each(Arr, Int, Fun)?
each_idx_val(e:Eachable1, cb:Fun) Source: stdlib.ngs:3307
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"))
each_ik_val(e:Eachable1, cb:Fun) Source: stdlib.ngs:4473
Same as each_idx_val() but only for e that is not Int or Str. Mnemonic: each index-or-key value.

Returns

e
ensure(e:Eachable1, p:Present) Source: stdlib.ngs:4802
Ensure a value is present. Does e.push(p.val) conditionally.

Example

[1,2].ensure(Present(3)) == [1,2,3]
[1,2].ensure(Present(2)) == [1,2]
fields(e:Eachable1, pat:Any) Source: stdlib.ngs:4236
Only keep specified fields in each element of e In some other languages, similar functionality is called "project".

Example

[{"a": 1, "b": 2}].fields(%[a])  # [{"a": 1}]
filter(e:Eachable1, pattern:Any=method) Source: stdlib.ngs:1664
Filter e using pattern.

Parameters

eEachable1. WARNING: instances of same type as e must have empty constructor and push() method.
patternWhen item =~ pattern 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]
first(e:Eachable1, pattern:Any=method, dflt:Any=block) Source: stdlib.ngs:1701
Find first element in e that matches the pattern.

Parameters

patternTest function or anything else acceptable by =~, defaults to Bool.constructors
dfltDefault value. If dflt is a Fun, the result of dflt() is used instead.

Returns

Either the element or Value(dflt) 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
[false, 0, null, "xyz", 100].first()  # "xyz"
[].first()                            # ElementNotFound exception
flat_map(e:Eachable1, mapper:Fun) Source: stdlib.ngs:1587
Map and flatten. Applies mapper to each element of e, collects results, and flattens the results (one level).

Example

[2,0,4].flat_map(F(e) Result({ 12 / e}).Box())  # [6, 3]
[2,0,4].flat_map(F(e) [1, e*2])  # [1,4, 1,0, 1,8]
flatten(e:Eachable1) Source: stdlib.ngs:3413
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:3128
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:3141
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, k:Fun, v:Fun=method) Source: stdlib.ngs:4388
Group items from e by key returned by k

Returns

Hash with Arr values

Example

["ab", "ac", "ba", "bc", "bd"].group(F(x) x[0])  # {a=[ab,ac], b=[ba,bc,bd]}
[["a", 1], ["b", 2], ["a", 3]].group(X[0], X[1])  # {"a": [1,3], "b": [2]}
group(e:Eachable1, field:Str) Source: stdlib.ngs:4403
Group items from e by the given field

Returns

Hash with Arr values

Example

[{"id": 100, "a": 1}, {"id": 200, "a": 2}, {"id": 300, "a": 2}].group("a")  # {1=[{id=100, a=1}], 2=[{id=200, a=2},{id=300, a=2}]}
has_index(e:Eachable1, i:Int) Source: stdlib.ngs:1990
Check whether Eachable1 has the given index: -len(e) <= i < len(e)

Returns

Bool
Hash(e:Eachable1, field:Str) Source: stdlib.ngs:4270
Create Hash with keys being the given field of elements and the values being corresponding elements.

Returns

Hash

Example

Hash([{'x': 1}, {'x': 2}], 'x')  # {1: {'x': 1}, 2: {'x': 2}}
Hash(e:Eachable1, cb:Fun) Source: stdlib.ngs:4281
Create a Hash from keys in Eachable1 using cb for values calculation

Parameters

eKeys of the hash to build
cbFunction to be called with one key at a time. Should calculate a value for the given key.

Returns

Hash

Example

Hash([1,2], F(x) x*2)  # {1: 2, 2: 4}
Hash(e:Eachable1, key_field:Str, val_field:Str) Source: stdlib.ngs:4305
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"}
Hash(e:Eachable1)experimental Source: stdlib.ngs:4314
Undocumented
in(x:Any, e:Eachable1) Source: stdlib.ngs:1940
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
indexes(e:Eachable1, pattern:Any=method) Source: stdlib.ngs:3476
Find all indexes of values that match the pattern

Parameters

patternTest function or anything else acceptable by (=~), defaults to Bool.constructors

Returns

Arr of Int

Example

[1,5,1,10].indexes(X>2)  # [1,3]
init(al:ArrLike, e:Eachable1) Source: stdlib.ngs:2047
Undocumented
init(s:Set, e:Eachable1) Source: stdlib.ngs:2400
Convert array to set
init(s:Stats, e:Eachable1) Source: stdlib.ngs:5560
Makes Stats, with each element in the array counted as if push()ed

Example

Stats(['a', 'a', 'b']).Hash()  # {'a': 2, 'b': 1}
Stats("AABC").Hash()  # {A=2, B=1, C=1}
init(c:_Cell, val:Eachable1) Source: autoload/globals/Table2.ngs:55
An Eachable1 value is a table cell with all elements one below the other. Converts elements to strings using _Cell(). Elements are separated by "---" when at least one of them is more than one line.
inspect(path:Arr, e:Eachable1)internal Source: stdlib.ngs:659
Internal method. Please do not use. Inspect Eachable1

Returns

Arr of Str
JsonData(e:Eachable1) Source: stdlib.ngs:2004
Convert Eachable1 into JSON compatible data structure - array
last(e:Eachable1, pattern:Any=method, dflt:Any=block) Source: stdlib.ngs:1723
Find last element in e that matches the pattern.

Parameters

patternTest function or anything else acceptable by =~, defaults to Bool.constructors
dfltDefault value. If dflt is a Fun, the result of dflt() is used instead.

Returns

Either the element or Value(dflt) if element was not found.

Example

[1,2,3].last(X>1, 10) == 3
[].last()  # ElementNotFound exception
map_idx_val(e:Eachable1, mapper:Fun) Source: stdlib.ngs:3328
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:1605
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:3762
Find maximal element under (>)
min(e:Eachable1) Source: stdlib.ngs:3760
Find minimal element under (<)
none(e:Eachable1, pattern:Any=method) Source: stdlib.ngs:3264
Check whether there is no element in e that satisfies the given pattern. Exactly same as not(any(e, pattern)) .

Parameters

eThe items to check
patternTest function or anything else acceptable by (=~), defaults to Bool.constructors

Returns

Bool

Example

[0,1,2].none(X>2)  # true
[0,1,2].none(X<2)  # false
partition(something:Eachable1, pattern:Any=method) Source: stdlib.ngs:1681
Partition elements in something according to the pattern into two arrays. First array contains all elements that match the pattern. The second array contains all elements that do not match the pattern.

Parameters

somethingEachable1
patternTest function or anything else acceptable by (=~), defaults to Bool.constructors

Returns

Array with two arrays in it

Example

partition([1,10,2,20], X>=10)  # [[10, 20], [1, 2]]
peach(e:Eachable1, *args:Arr) Source: stdlib.ngs:5758
Parallel each. Same as pmap but returns the original
pfilter(e:Eachable1, pattern:Any) Source: stdlib.ngs:5774
Parallel filter. Typically should be used with "heavy" pattern such as SSH.

Returns

Of same type as e

Example

Set(1..10).pfilter(X>3)  # <Set 4, 5, 6, 7, 8, 9>
pfilter(e:Eachable1, threads:Int, pattern:Any) Source: stdlib.ngs:5785
Parallel filter. Typically should be used with "heavy" pattern such as SSH.

Returns

Of same type as e

Example

Set(1..10).pfilter(2, X>3)  # <Set 4, 5, 6, 7, 8, 9>
pmap(e:Eachable1, mapper:Fun) Source: stdlib.ngs:5709
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). Number of threads is limited to the lower of the two: processors * 100 or 1000.

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`)
pmap(e:Eachable1, threads:Int, mapper:Fun) Source: stdlib.ngs:5730
Parallel map. Runs mapper in "threads" total threads.

Parameters

eEachable1 to be mapped
threadsnumber of threads to use
mappercalled with elements from e

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:1948
Push all elements of e into dst.

Returns

dst
reduce(something:Eachable1, start:Any, f:Fun) Source: stdlib.ngs:1843
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:1861
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((+))
reject(something:Eachable1, pattern:Any=method) Source: stdlib.ngs:1782
Filter something to an Arr (array) of values matching the pattern

Parameters

somethingobject of any type that has each(something, callback) implemented
patternDecision function to be called with each item as first argument. When item matches the pattern, the item will not appear in the resulting array

Returns

Arr

Example

(1...10).reject(F(num) num % 2 == 0)  # Gives [1,3,5,7,9]
(1...10).reject(X<5)  # Gives [5,6,7,8,9,10]
replace(e:Eachable1, src:Any, dst:Any) Source: stdlib.ngs:1828
Replace all occurrences of src with dst TODO: Return same type, not Arr

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:3875
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:3513
Calculate sum of the elements

Example

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

Example

[1,2,3,1,2].take(X<3)  # [1,2]
the_one(e:Eachable1, pattern:Any=method, body:Fun=method, found_more:Any=block, found_none:Any=block) Source: stdlib.ngs:1747
Find the only element that matches the pattern and execute given code with the value

Parameters

bodyThe code to execute when exactly one element that matches the pattern was found. Executed with the found value. It's value will be returned as result of the_one().
found_moreThe value to return when more than one element matches the pattern. If found_more is a Fun, the result of found_more() is returned instead. Defaults to throwing ElementNotFound.
found_noneThe value to return when none of the elements match the pattern. If found_none is a Fun, the result of found_none() is returned instead. Defaults to throwing ElementNotFound.

Returns

One of the following: body(the_element), Value(found_more), Value(found_none)

Example

F name(dn:MethodDocNode) {
	dn.children.the_one(MethodNameDocNode).text[0]
}
uniq(e:Eachable1, cb:Fun=method) Source: stdlib.ngs:3423
Return unique values. Warning: uses Hash so comparison is not using == but a built-in hash keys comparison.

Returns

Of same type as e

Example

[1,2,2,3,4,4].uniq()  # [1,2,3,4]
[{"a": 1, "z": 10}, {"a": 1, "z": 20}, {"a": 2, "z": 30}].uniq(X.a)  # [{"a": 1, "z": 10}, {"a": 2, "z": 30}]
uniq(e:Eachable1, field:Str) Source: stdlib.ngs:3450
Return unique values. Warning: uses Hash so comparison is not using == but a built-in hash keys comparison.

Returns

Of same type as e

Example

[{"id": 100, "a": 1}, {"id": 200, "a": 2}, {"id": 300, "a": 2}].uniq("a")  # [{"id": 100, "a": 1}, {"id": 200, "a": 2}]
without(e:Eachable1, without_elt:Any)deprecated Source: stdlib.ngs:3377
Filter out all occurrences of specific value Deprecated. Use reject().

Parameters

eitems to filter
without_eltThe value to filter out

Returns

Arr

Example

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