Skip to content

Commit

Permalink
(objectionary#1207) - Extract ExReduceBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoss committed Sep 18, 2022
1 parent bdf094d commit 3784756
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 59 deletions.
24 changes: 4 additions & 20 deletions eo-runtime/src/main/java/EOorg/EOeolang/EObytes$EOand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import org.eolang.AtVararg;
import org.eolang.Bytes;
import org.eolang.BytesOf;
import org.eolang.Dataized;
import org.eolang.ExFailure;
import org.eolang.ExprReduce;
import org.eolang.Param;
import org.eolang.PhDefault;
import org.eolang.Phi;
Expand All @@ -58,25 +57,10 @@ public class EObytes$EOand extends PhDefault {
"φ",
new AtComposite(
this,
rho -> {
Bytes base = new Param(rho).asBytes();
final Phi[] args = new Param(rho, "b").strong(Phi[].class);
for (int index = 0; index < args.length; ++index) {
final Object val = new Dataized(args[index]).take();
if (!(val instanceof byte[])) {
throw new ExFailure(
String.format(
"The %dth argument of 'and' is of type %s, not bytes",
index, val.getClass().getCanonicalName()
)
);
}
base = base.and(new BytesOf(byte[].class.cast(val)));
}
return Bytes.toPhi(base);
}
new ExReduceBytes(
Bytes::and
)
)
);
}

}
30 changes: 6 additions & 24 deletions eo-runtime/src/main/java/EOorg/EOeolang/EObytes$EOor.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import org.eolang.AtVararg;
import org.eolang.Bytes;
import org.eolang.BytesOf;
import org.eolang.Dataized;
import org.eolang.ExFailure;
import org.eolang.ExprReduce;
import org.eolang.Param;
import org.eolang.PhDefault;
import org.eolang.Phi;
Expand All @@ -43,16 +42,13 @@
*
* @since 1.0
* @checkstyle TypeNameCheck (15 lines)
* @todo #1184:30m This object is almost identical to
* EOand & EOxor. Extract method for converting Phy[]
* to Bytes[] to Param. And replace duplicated
* part this call to this method.
*/
@XmirObject(oname = "bytes.or")
public class EObytes$EOor extends PhDefault {

/**
* Ctor.
*
* @param sigma Sigma
*/
public EObytes$EOor(final Phi sigma) {
Expand All @@ -62,25 +58,11 @@ public class EObytes$EOor extends PhDefault {
"φ",
new AtComposite(
this,
rho -> {
Bytes base = new Param(rho).asBytes();
final Phi[] args = new Param(rho, "b").strong(Phi[].class);
for (int index = 0; index < args.length; ++index) {
final Object val = new Dataized(args[index]).take();
if (!(val instanceof byte[])) {
throw new ExFailure(
String.format(
"The %dth argument of 'and' is of type %s, not bytes",
index, val.getClass().getCanonicalName()
)
);
}
base = base.or(new BytesOf(byte[].class.cast(val)));
}
return Bytes.toPhi(base);
}
new ExReduceBytes(
Bytes::or
)
)
);
}

}

18 changes: 3 additions & 15 deletions eo-runtime/src/main/java/EOorg/EOeolang/EObytes$EOxor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
import org.eolang.AtComposite;
import org.eolang.AtVararg;
import org.eolang.Bytes;
import org.eolang.BytesOf;
import org.eolang.Dataized;
import org.eolang.Param;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.XmirObject;
Expand All @@ -57,18 +54,9 @@ public class EObytes$EOxor extends PhDefault {
"φ",
new AtComposite(
this,
rho -> {
Bytes base = new Param(rho).asBytes();
final Phi[] args = new Param(rho, "b").strong(Phi[].class);
for (final Phi phi : args) {
base = base.xor(
new BytesOf(
new Dataized(phi).take(byte[].class)
)
);
}
return Bytes.toPhi(base);
}
new ExReduceBytes(
Bytes::xor
)
)
);
}
Expand Down
70 changes: 70 additions & 0 deletions eo-runtime/src/main/java/EOorg/EOeolang/ExReduceBytes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 Objectionary.com
*
* 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.
*/

/*
* @checkstyle PackageNameCheck (4 lines)
*/
package EOorg.EOeolang;

import java.util.function.BinaryOperator;
import org.eolang.Bytes;
import org.eolang.BytesOf;
import org.eolang.Dataized;
import org.eolang.Expr;
import org.eolang.Param;
import org.eolang.Phi;

/**
* Reduce on BYTES.
*
* @since 1.0
*/
final class ExReduceBytes implements Expr {
/**
* Reduce operation.
*/
private final BinaryOperator<Bytes> oper;

/**
* Ctor.
* @param oper Operation.
*/
ExReduceBytes(final BinaryOperator<Bytes> oper) {
this.oper = oper;
}

@Override
public Phi get(final Phi rho) throws Exception {
Bytes base = new Param(rho).asBytes();
final Phi[] args = new Param(rho, "b").strong(Phi[].class);
for (final Phi phi : args) {
base = this.oper.apply(base,
new BytesOf(
new Dataized(phi).take(byte[].class)
)
);
}
return Bytes.toPhi(base);
}
}

0 comments on commit 3784756

Please sign in to comment.