From 3425275ce072e22bbdc9ace0a25fa09d101292f4 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 23 Feb 2021 21:28:31 +0300 Subject: [PATCH] #273: array.each --- eo-runtime/src/main/eo/org/eolang/array.eo | 3 + .../main/java/org/eolang/EOarray$EOeach.java | 64 +++++++++++++++++++ .../java/org/eolang/EOarray$EOreduce.java | 2 +- .../src/test/eo/org/eolang/array-tests.eo | 11 +++- 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 eo-runtime/src/main/java/org/eolang/EOarray$EOeach.java diff --git a/eo-runtime/src/main/eo/org/eolang/array.eo b/eo-runtime/src/main/eo/org/eolang/array.eo index 0425724e07..21419db51c 100644 --- a/eo-runtime/src/main/eo/org/eolang/array.eo +++ b/eo-runtime/src/main/eo/org/eolang/array.eo @@ -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 diff --git a/eo-runtime/src/main/java/org/eolang/EOarray$EOeach.java b/eo-runtime/src/main/java/org/eolang/EOarray$EOeach.java new file mode 100644 index 0000000000..cda88ce8d9 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/EOarray$EOeach.java @@ -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); + }))); + } + +} diff --git a/eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java b/eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java index 5d3c8fd5fc..c30d16e91b 100644 --- a/eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java +++ b/eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java @@ -32,7 +32,7 @@ import org.eolang.phi.Phi; /** - * IF. + * REDUCE. * * @since 1.0 */ diff --git a/eo-runtime/src/test/eo/org/eolang/array-tests.eo b/eo-runtime/src/test/eo/org/eolang/array-tests.eo index 5e18aa29a2..65b0da231f 100644 --- a/eo-runtime/src/test/eo/org/eolang/array-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/array-tests.eo @@ -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 @@ -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