Using the constant function #1735
-
Hello, I recently found the constant function, and I noticed a weird behavior (at least from my point of view, so I might have misunderstood its usage). import * as O from "fp-ts/Option";
import { constant, pipe } from "fp-ts/function";
pipe(
O.some("hello"),
O.fold(constant(console.log("none")), constant(console.log("some")))
) You will notice that it outputs both "none" and "some" in the terminal. import * as O from "fp-ts/Option";
import { pipe } from "fp-ts/function";
pipe(
O.some("hello"),
O.fold(() => console.log("none"), () => console.log("some"))
) Which just outputs "some", I'm still a little bit confused on why this happens because the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'd say, you nearly gave the answer by your self: |
Beta Was this translation helpful? Give feedback.
I'd say, you nearly gave the answer by your self:
constant
function returnsLazy<A>
. In your first example theconsole.log("none")
andconsole.log("some")
will be executed instantly and their return values (void
in this case) becomes lazy.