Re: [Haskell-cafe] Substituting values

2012-12-26 Thread Chaddaï Fouché
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

Re: [Haskell-cafe] Substituting values

2012-12-25 Thread Radical
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 } :

Re: [Haskell-cafe] Substituting values

2012-12-24 Thread Radical
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.

Re: [Haskell-cafe] Substituting values

2012-12-24 Thread Radical
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]

Re: [Haskell-cafe] Substituting values

2012-12-22 Thread David Thomas
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 (==

Re: [Haskell-cafe] Substituting values

2012-12-22 Thread Petr P
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]({

Re: [Haskell-cafe] Substituting values

2012-12-22 Thread Jason Dusek
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

[Haskell-cafe] Substituting values

2012-12-21 Thread Radical
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

Re: [Haskell-cafe] Substituting values

2012-12-21 Thread Kim-Ee Yeoh
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.