Traditional FP algorithms and side effects #1030
Replies: 1 comment
-
Really, no. You yourself go on to say next that your notion of purity allows non-memory side-effects. And really, hylo's type system rules out arbitrary memory side-effects. What we're really left with is that it's a way to specify that a lambda is not mutated when it is called. The fact that you are trying to show a case with implicit I'm guessing you meant: fun g(_ a: Int) inout -> Int {
(a.copy(), &x += x).0
} Which I might like to be written as: fun [inout x] g(_ a: Int) inout -> Int {
(a.copy(), &x += x).0
} The point I'm trying to make here is that we're very explicit about memory side-effects in Hylo. So from that point of view, I have no problem whatsoever with the idea that functional algorithms can mutate the functions they operate on. The key is that it's the responsibility of a function not to change its return value based on internal state. It would be interesting if we could enforce that in the language. |
Beta Was this translation helpful? Give feedback.
-
In Hylo there's a way to specify that a lambda shall not have side effects:
There are a couple of very well established operations in the FP world that we'll certainly want to bring to Hylo. One of them is mapping, which consists of applying a function on each element of a sequence. Such a function is typically called
map
.Because
map
comes from the functional world, one may wonder whether it should accept impure functions. If it does, one could write code that may look quite puzzling to a functional programmer:If
map
required its argument to be pure, I think we would need another idiomatic way to apply an "effectful map" without having to use pure imperative style. Perhaps something likemap(mutating:_:)
. We tried this approach inhc
's code base. Though the result was not very convincing, I am tempted to give it a second shot in Hylo, because the type system has a way to better enforce some form of purity.Note that the same observations apply to other traditional FP algorithms, like
fold
andfilter
.Beta Was this translation helpful? Give feedback.
All reactions