You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
or at least move unsafe API under a Unsafe nested class (or better: a separate "unsafe" artefact) with all methods prefixed with unsafe.
eg. List.tail(), List.head() should not be part of List public API.
I do find myself using these regularly because Java lacks a pattern matching mechanism. I think it could be done using the visitor pattern, but List doesn't currently implement the visitor pattern. So I do something like if(x.isNotEmpty()) { ... x.head() ... } else { ... }.
Removing or renaming these methods now would create some headaches for me, and probably others, so some kind of deprecation procedure would be recommended. I'm not sure its worth the pain right now.
I do find myself using these regularly because Java lacks a pattern
matching mechanism. I think it could be done using the visitor
pattern, but List doesn't currently implement the visitor pattern. So
I do something like |if(x.isNotEmpty()) { ... x.head() ... } else {
... }|.
the road block to completely removing those unsafe functions is the lack of TCO. Eg. Implementing listEqual without head() and tail() in a stack safe manner is not totally satisfying: you have to allocate objects (Option or Trampolining) at each iteration, or use some sort of mutable local function.
But well this is Java...
I am personally ok-ish with the former implementation but I am guessing not everyone... @functionaljava WDYT?
or is there alternative I am not seeing?
one of them may be to keep using head/tail for primitive recursive functions...
I think it's good to leave the door open to optimization. Maybe just rename
them to have an underscore in front, so people feel a bit uncomfortable
using them?
To clarify what I meant by "some sort of mutable function", here is how
one could implement List.forEach without head() and tail():
public final Unit foreach(final F<A, Unit> f) {
final class ForEachCons implements F2<A, List, Boolean> {
List as = List.this; @OverRide
public Boolean f(A head, List tail) {
as = tail;
f.f(head);
return true;
}
}
ForEachCons forEachCons = new ForEachCons();
while (forEachCons.as.uncons(forEachCons, false)) {}
return unit();
}
To be compared with current implementation:
public final Unit foreach(final F<A, Unit> f) {
for (List xs = this; xs.isNotEmpty(); xs = xs.tail()) {
f.f(xs.head());
}
return unit();
}
I am personally ok-ish with the former implementation but I am guessing
not everyone... @functionaljavahttps://github.com/functionaljava WDYT?
or is there alternative I am not seeing?
one of them may be to keep using head/tail for primitive recursive
functions...
I think it would also be good to give NonEmptyList more prominence (and full List methods)... for instance, List.groupBy should produce TreeMap<B, NonEmptyList<A>>.
+1 from me for this issue. It is hard for me to sell the library to my coworkers as a safe alternative to the Java standard library if it provides unsafe operations.
Activity
dobesv commentedon Sep 11, 2015
I do find myself using these regularly because Java lacks a pattern matching mechanism. I think it could be done using the visitor pattern, but List doesn't currently implement the visitor pattern. So I do something like
if(x.isNotEmpty()) { ... x.head() ... } else { ... }.Removing or renaming these methods now would create some headaches for me, and probably others, so some kind of deprecation procedure would be recommended. I'm not sure its worth the pain right now.
techtangents commentedon Sep 11, 2015
tonymorris commentedon Sep 12, 2015
👍
Move unsafe functions out, so that if they are not imported, it is more clear that they are not used.
jbgi commentedon Oct 22, 2016
the road block to completely removing those unsafe functions is the lack of TCO. Eg. Implementing listEqual without
head()andtail()in a stack safe manner is not totally satisfying: you have to allocate objects (Option or Trampolining) at each iteration, or use some sort of mutable local function.But well this is Java...
jbgi commentedon Oct 22, 2016
To clarify what I meant by "some sort of mutable function", here is how one could implement
List.forEachwithouthead()andtail():To be compared with current implementation:
I am personally ok-ish with the former implementation but I am guessing not everyone... @functionaljava WDYT?
or is there alternative I am not seeing?
one of them may be to keep using
head/tailfor primitive recursive functions...dobesv commentedon Oct 25, 2016
I think it's good to leave the door open to optimization. Maybe just rename
them to have an underscore in front, so people feel a bit uncomfortable
using them?
On Sat, Oct 22, 2016, 4:02 AM JB Giraudeau notifications@github.com wrote:
siro-mateos commentedon Dec 13, 2016
I think it would also be good to give
NonEmptyListmore prominence (and fullListmethods)... for instance,List.groupByshould produceTreeMap<B, NonEmptyList<A>>.devkat commentedon Feb 18, 2021
+1 from me for this issue. It is hard for me to sell the library to my coworkers as a safe alternative to the Java standard library if it provides unsafe operations.