Skip to content

Commit

Permalink
#273: array.each
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 23, 2021
1 parent 2c1091e commit 3425275
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
3 changes: 3 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/array.eo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# Reduce from start "a" using the function "f"
[a f] > reduce /a

# For each array element dataize the object
[f] > each /bool

# Map with index. Here "f" must be an abstract
# object with two free attributes. The first
# one for the element of the array, the second one
Expand Down
64 changes: 64 additions & 0 deletions eo-runtime/src/main/java/org/eolang/EOarray$EOeach.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2021 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.eolang;

import org.eolang.phi.AtBound;
import org.eolang.phi.AtFree;
import org.eolang.phi.AtLambda;
import org.eolang.phi.Data;
import org.eolang.phi.Datarized;
import org.eolang.phi.PhCopy;
import org.eolang.phi.PhDefault;
import org.eolang.phi.PhWith;
import org.eolang.phi.Phi;

/**
* EACH.
*
* @since 1.0
*/
public class EOarray$EOeach extends PhDefault {

public EOarray$EOeach(final Phi parent) {
super(parent);
this.add("f", new AtFree());
this.add("φ", new AtBound(new AtLambda(this, self -> {
final Phi[] array = new Datarized(
self.attr("ρ").get()
).take(Phi[].class);
for (final Phi item : array) {
new Datarized(
new PhWith(
new PhCopy(self.attr("f").get()),
0,
item
)
).take();
}
return new Data.ToPhi(true);
})));
}

}
2 changes: 1 addition & 1 deletion eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.eolang.phi.Phi;

/**
* IF.
* REDUCE.
*
* @since 1.0
*/
Expand Down
11 changes: 10 additions & 1 deletion eo-runtime/src/test/eo/org/eolang/array-tests.eo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
+package org.eolang
+alias sprintf org.eolang.txt.sprintf
+alias org.eolang.txt.sprintf
+alias org.eolang.io.stdout
+junit

[] > makes-array-through-special-syntax
Expand All @@ -12,6 +13,14 @@
0
*.length

[] > iterates-with-each
*
"one"
"two"
"three"
.each > @
[i] (stdout i > @)

# check that an empty array's .length equals to zero
[] > emptyArrayLengthTest
[elements...] > arr
Expand Down

0 comments on commit 3425275

Please sign in to comment.