Since recently, the notion of prisms from the lens library can achieve
that : to modify a value only in certain conditions but you have to
write the prism so it's not that convenient, though at least you'll
have an uniform API.
See
Hey Petr,
On Sat, Dec 22, 2012 at 9:52 AM, Petr P petr@gmail.com wrote:
I think you need something wha Scala has - the ability to create a partial
function from a case expression. In Scala you could write
def update[A](f: PartialFunction[A,A])(v: A): A =
f.orElse({ case x = x } :
On Sat, Dec 22, 2012 at 12:35 AM, Kim-Ee Yeoh k...@atamo.com wrote:
This is reminiscent of the Either (exception) monad where Left values, the
exceptions, pass through unaltered, and Right values are transformable,
i.e. acted on by functions.
Interesting. I hadn't thought of that parallel.
Yeah, I guess I was wondering whether something like it already existed.
Thinking about it some more, perhaps what I want is `tr` [1]. E.g.
tr [Foo] [Bar]
Incorporating your suggestion would then yield a more general version like:
trBy (== Foo) Bar
[1]
Seems like the function is easy to define:
replaceIfEq a b c = if c == a then b else c
Then the above can be written
replaceIfEq Foo Bar value
Or the slightly more general (in exchange for slightly more verbosity at
the call site)
replaceIf p r a = if p a then r else a
replaceIf (==
Hi Alvaro,
I think you need something wha Scala has - the ability to create a partial
function from a case expression. In Scala you could write
def update[A](f: PartialFunction[A,A])(v: A): A =
f.orElse({ case x = x } : PartialFunction[A,A]).apply(v);
and then use it like
update[Int]({
2012/12/21 Radical radi...@google.com:
Sometimes I'll need something like:
if value == Foo then Bar else value
Or some syntactic variation thereof:
case value of { Foo - Bar; _ - value }
Is there a better/shorter way to do it? I'm surprised that it's more
complicated to substitute a
Sometimes I'll need something like:
if value == Foo then Bar else value
Or some syntactic variation thereof:
case value of { Foo - Bar; _ - value }
Is there a better/shorter way to do it? I'm surprised that it's more
complicated to substitute a value on its own than e.g. in a list, using
This is reminiscent of the Either (exception) monad where Left values, the
exceptions, pass through unaltered, and Right values are transformable,
i.e. acted on by functions.
But I have no idea what you're trying to achieve in the bigger picture.
Help us help you by fleshing out your use case.