Re: Scala for-comprehension to Clojure

2015-11-26 Thread Mark Engelberg
Most Scala `for` examples translate over to Clojure's `for`.  The biggest
difference is that Scala produces a collection matching the type of the
first generator, whereas Clojure produces a lazy sequence, which you can
"pour" into the collection of your choice with `into`.

It looks like your specific example is also using some sort of imperative
generator in Scala.  Clojure tends to frown on those sorts of constructs,
and is more prescriptive, guiding you towards a more functional solution.
You might rework that generator as a lazy sequence.  Or possibly, if you
really need something more imperative in nature, you could represent the
generator as a channel and you could accomplish what you want with
transducers on the channel (I'm not aware of a for-like macro that operates
on channels, but it seems theoretically possible to write such a thing).


On Thu, Nov 26, 2015 at 8:16 AM, Rastko Soskic  wrote:

> Hi, I am not sure if it will serve point the best but that is what I have
> received as a koan style problem with Scala solution,
> now I am trying to comprehend it fully and convert to Clojure. Like I said
> I am by no means Scala expert.
>
> Let's say we have some Val container which is capable to hold some value
> plus yield another value
> which depends on "mapping" function provided to Val itself (exec param of
> case class).
>
> package question.google.group
>
> object worksheet {
>   def unit[S, A](a: A): Val[S, A] =
> Val(s => (a, s))
>
>   case class Val[S, +A](exec: S => (A, S)) {
> def map[B](f: A => B): Val[S, B] =
>   flatMap(a => unit(f(a)))
> def flatMap[B](f: A => Val[S, B]): Val[S, B] = Val(s => {
>   val (a, s1) = exec(s)
>   f(a).exec(s1)
> })
>   }
>
>   def int : Val[Int, Int] = Val(x => (x, x + 1))
>
> *  val something = for {*
> *x <- int*
> *y <- int*
> *  } yield (x, y)  //> something
>  : question.google.group.Val[Int,(Int, Int)] = Val()*
>
>   something.exec(5)   //> res9: ((Int, Int),
> Int) = ((5,6),7)
> }
>
> If I am following this example and wikipedia well (on Monads in fp),
> in bottom line Val is a monad given presence of type unit and bind
> (flatMap) operation... But that is not my primary focus here.
>
> It is Scala's for comprehension which expands to map and flatMap calls so
> some intial value (given to exec function) "flows" through it
> yielding in the end expected result.
>
> I hope this sheds some more light on what I am seeking for.
>
> @Torsten, thanks for mentioned resources I'll take a look at them.
>
> On Thursday, November 26, 2015 at 12:40:50 PM UTC+1, Gary Verhaegen wrote:
>>
>> It's a bit hard (at least for me) to see what you're actually trying to
>> do here that would precent a direct translation of your snippet to
>> Clojure's for. Could you perhaps post a complete, self-contained code
>> example in Scala?
>>
>> On Thursday, 26 November 2015, Torsten Uhlmann 
>> wrote:
>>
>>> Hi Rastko,
>>>
>>> One way of doing that would be to use the mlet macro from the Cats
>>> library: http://funcool.github.io/cats/latest/#mlet
>>>
>>> Also, there are several if-lets or when-lets out there that allow
>>> multiple bindings, I used one from https://github.com/ptaoussanis/encore
>>>
>>> I use Scala's for most of the time when there are Options in the mix
>>> that may or may not hold a value.
>>>
>>> For binding to generators, Clojures for might be a better fit?
>>> https://clojuredocs.org/clojure.core/for
>>>
>>> Would that help you?
>>>
>>> Torsten.
>>>
>>> PS: I'm learning Clojure myself with Scala and Java background.
>>>
>>> On Thursday, November 26, 2015 at 12:12:01 PM UTC+1, Rastko Soskic wrote:
>>>>
>>>> Hi,
>>>> I am aware of philosophical differences of Scala and Clojure
>>>> but functional programming should be a pretty common ground :)
>>>> Thus I need help, I am trying to mimic Scala's for comprehension in
>>>> Clojure.
>>>>
>>>> Hopefully someone will be able to aid me with the following (perhaps
>>>> more familiar with Scala):
>>>>
>>>> Scala for comprehension is errr how to say "de-sugared" to series
>>>> of map and flatMap calls... thus, it is possible to use plain functions
>>>> in for-comprehension
>>>> like:
>>>> *val *ns: Conv[List[Int]] = *for *{ // Conv is j

Re: Scala for-comprehension to Clojure

2015-11-26 Thread Rastko Soskic
Hi, I am not sure if it will serve point the best but that is what I have 
received as a koan style problem with Scala solution,
now I am trying to comprehend it fully and convert to Clojure. Like I said 
I am by no means Scala expert.

Let's say we have some Val container which is capable to hold some value 
plus yield another value 
which depends on "mapping" function provided to Val itself (exec param of 
case class).

package question.google.group

object worksheet {
  def unit[S, A](a: A): Val[S, A] =
Val(s => (a, s)) 

  case class Val[S, +A](exec: S => (A, S)) {
def map[B](f: A => B): Val[S, B] =
  flatMap(a => unit(f(a)))
def flatMap[B](f: A => Val[S, B]): Val[S, B] = Val(s => {
  val (a, s1) = exec(s)
  f(a).exec(s1)
})
  }
  
  def int : Val[Int, Int] = Val(x => (x, x + 1)) 

*  val something = for {*
*x <- int*
*y <- int*
*  } yield (x, y)  //> something 
 : question.google.group.Val[Int,(Int, Int)] = Val()*
  
  something.exec(5)   //> res9: ((Int, Int), 
Int) = ((5,6),7)
}

If I am following this example and wikipedia well (on Monads in fp), 
in bottom line Val is a monad given presence of type unit and bind 
(flatMap) operation... But that is not my primary focus here.

It is Scala's for comprehension which expands to map and flatMap calls so 
some intial value (given to exec function) "flows" through it
yielding in the end expected result.

I hope this sheds some more light on what I am seeking for.

@Torsten, thanks for mentioned resources I'll take a look at them.

On Thursday, November 26, 2015 at 12:40:50 PM UTC+1, Gary Verhaegen wrote:
>
> It's a bit hard (at least for me) to see what you're actually trying to do 
> here that would precent a direct translation of your snippet to Clojure's 
> for. Could you perhaps post a complete, self-contained code example in 
> Scala?
>
> On Thursday, 26 November 2015, Torsten Uhlmann  > wrote:
>
>> Hi Rastko,
>>
>> One way of doing that would be to use the mlet macro from the Cats 
>> library: http://funcool.github.io/cats/latest/#mlet
>>
>> Also, there are several if-lets or when-lets out there that allow 
>> multiple bindings, I used one from https://github.com/ptaoussanis/encore
>>
>> I use Scala's for most of the time when there are Options in the mix that 
>> may or may not hold a value.
>>
>> For binding to generators, Clojures for might be a better fit? 
>> https://clojuredocs.org/clojure.core/for
>>
>> Would that help you?
>>
>> Torsten.
>>
>> PS: I'm learning Clojure myself with Scala and Java background.
>>
>> On Thursday, November 26, 2015 at 12:12:01 PM UTC+1, Rastko Soskic wrote:
>>>
>>> Hi, 
>>> I am aware of philosophical differences of Scala and Clojure
>>> but functional programming should be a pretty common ground :)
>>> Thus I need help, I am trying to mimic Scala's for comprehension in 
>>> Clojure.
>>>
>>> Hopefully someone will be able to aid me with the following (perhaps 
>>> more familiar with Scala):
>>>
>>> Scala for comprehension is errr how to say "de-sugared" to series 
>>> of map and flatMap calls... thus, it is possible to use plain functions 
>>> in for-comprehension
>>> like:
>>> *val *ns: Conv[List[Int]] = *for *{ // Conv is just alias for functions 
>>> of type B => (B, A)
>>> x <- int // int is function again of type Conv
>>> y <- int
>>> xs <- ints(x) // this is just sequence of numbers
>>> } *yield *xs.map(_ * y)
>>>
>>> I don't need all the nuts and bolts, just some guideline for achieving 
>>> something similar
>>> in Clojure.
>>>
>>> I am not lazy :) I've already eagerly researched a bit and got to this: 
>>> Scala 
>>> for-comprehension to Clojure 
>>> <http://stackoverflow.com/questions/25655132/how-to-convert-this-map-flatmap-into-a-for-comprehension-in-clojure>
>>>
>>> But as you can see that is not really about having kind of generator 
>>> function which is wrapped into flatMap
>>> call. Perhaps this is not in Clojure's spirit at all, perhaps there is 
>>> some Clojure idiom to achieve something similar.
>>>
>>> Any tip, suggestion, critic is welcome and appreciated.
>>>
>>> If someone is wondering how in the world I came up to this, I am doing 
>>> some f-p exercises which I've got Scala solution for
>>> however

Re: Scala for-comprehension to Clojure

2015-11-26 Thread Chris Murphy
It is the same thing in Clojure. It is called a 'list comprehension' even
although for form for it starts with `for`  I wouldn't worry too much about
the flatmap / mapcat stuff, as I think the translation should be quite
direct at the higher 'comprehension' level.
On Nov 26, 2015 10:12 PM, "Rastko Soskic"  wrote:

> Hi,
> I am aware of philosophical differences of Scala and Clojure
> but functional programming should be a pretty common ground :)
> Thus I need help, I am trying to mimic Scala's for comprehension in
> Clojure.
>
> Hopefully someone will be able to aid me with the following (perhaps more
> familiar with Scala):
>
> Scala for comprehension is errr how to say "de-sugared" to series
> of map and flatMap calls... thus, it is possible to use plain functions in
> for-comprehension
> like:
> *val *ns: Conv[List[Int]] = *for *{ // Conv is just alias for functions
> of type B => (B, A)
> x <- int // int is function again of type Conv
> y <- int
> xs <- ints(x) // this is just sequence of numbers
> } *yield *xs.map(_ * y)
>
> I don't need all the nuts and bolts, just some guideline for achieving
> something similar
> in Clojure.
>
> I am not lazy :) I've already eagerly researched a bit and got to this: Scala
> for-comprehension to Clojure
> <http://stackoverflow.com/questions/25655132/how-to-convert-this-map-flatmap-into-a-for-comprehension-in-clojure>
>
> But as you can see that is not really about having kind of generator
> function which is wrapped into flatMap
> call. Perhaps this is not in Clojure's spirit at all, perhaps there is
> some Clojure idiom to achieve something similar.
>
> Any tip, suggestion, critic is welcome and appreciated.
>
> If someone is wondering how in the world I came up to this, I am doing
> some f-p exercises which I've got Scala solution for
> however I am not very interested in Scala and I am doing Clojure so I just
> need to grasp concepts...
>
> Thanks in advance...
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Scala for-comprehension to Clojure

2015-11-26 Thread Gary Verhaegen
It's a bit hard (at least for me) to see what you're actually trying to do
here that would precent a direct translation of your snippet to Clojure's
for. Could you perhaps post a complete, self-contained code example in
Scala?

On Thursday, 26 November 2015, Torsten Uhlmann 
wrote:

> Hi Rastko,
>
> One way of doing that would be to use the mlet macro from the Cats
> library: http://funcool.github.io/cats/latest/#mlet
>
> Also, there are several if-lets or when-lets out there that allow multiple
> bindings, I used one from https://github.com/ptaoussanis/encore
>
> I use Scala's for most of the time when there are Options in the mix that
> may or may not hold a value.
>
> For binding to generators, Clojures for might be a better fit?
> https://clojuredocs.org/clojure.core/for
>
> Would that help you?
>
> Torsten.
>
> PS: I'm learning Clojure myself with Scala and Java background.
>
> On Thursday, November 26, 2015 at 12:12:01 PM UTC+1, Rastko Soskic wrote:
>>
>> Hi,
>> I am aware of philosophical differences of Scala and Clojure
>> but functional programming should be a pretty common ground :)
>> Thus I need help, I am trying to mimic Scala's for comprehension in
>> Clojure.
>>
>> Hopefully someone will be able to aid me with the following (perhaps more
>> familiar with Scala):
>>
>> Scala for comprehension is errr how to say "de-sugared" to series
>> of map and flatMap calls... thus, it is possible to use plain functions
>> in for-comprehension
>> like:
>> *val *ns: Conv[List[Int]] = *for *{ // Conv is just alias for functions
>> of type B => (B, A)
>> x <- int // int is function again of type Conv
>> y <- int
>> xs <- ints(x) // this is just sequence of numbers
>> } *yield *xs.map(_ * y)
>>
>> I don't need all the nuts and bolts, just some guideline for achieving
>> something similar
>> in Clojure.
>>
>> I am not lazy :) I've already eagerly researched a bit and got to this: Scala
>> for-comprehension to Clojure
>> <http://stackoverflow.com/questions/25655132/how-to-convert-this-map-flatmap-into-a-for-comprehension-in-clojure>
>>
>> But as you can see that is not really about having kind of generator
>> function which is wrapped into flatMap
>> call. Perhaps this is not in Clojure's spirit at all, perhaps there is
>> some Clojure idiom to achieve something similar.
>>
>> Any tip, suggestion, critic is welcome and appreciated.
>>
>> If someone is wondering how in the world I came up to this, I am doing
>> some f-p exercises which I've got Scala solution for
>> however I am not very interested in Scala and I am doing Clojure so I
>> just need to grasp concepts...
>>
>> Thanks in advance...
>>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> 
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> 
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Scala for-comprehension to Clojure

2015-11-26 Thread Torsten Uhlmann
Hi Rastko,

One way of doing that would be to use the mlet macro from the Cats library: 
http://funcool.github.io/cats/latest/#mlet

Also, there are several if-lets or when-lets out there that allow multiple 
bindings, I used one from https://github.com/ptaoussanis/encore

I use Scala's for most of the time when there are Options in the mix that 
may or may not hold a value.

For binding to generators, Clojures for might be a better 
fit? https://clojuredocs.org/clojure.core/for

Would that help you?

Torsten.

PS: I'm learning Clojure myself with Scala and Java background.

On Thursday, November 26, 2015 at 12:12:01 PM UTC+1, Rastko Soskic wrote:
>
> Hi, 
> I am aware of philosophical differences of Scala and Clojure
> but functional programming should be a pretty common ground :)
> Thus I need help, I am trying to mimic Scala's for comprehension in 
> Clojure.
>
> Hopefully someone will be able to aid me with the following (perhaps more 
> familiar with Scala):
>
> Scala for comprehension is errr how to say "de-sugared" to series 
> of map and flatMap calls... thus, it is possible to use plain functions in 
> for-comprehension
> like:
> *val *ns: Conv[List[Int]] = *for *{ // Conv is just alias for functions 
> of type B => (B, A)
> x <- int // int is function again of type Conv
> y <- int
> xs <- ints(x) // this is just sequence of numbers
> } *yield *xs.map(_ * y)
>
> I don't need all the nuts and bolts, just some guideline for achieving 
> something similar
> in Clojure.
>
> I am not lazy :) I've already eagerly researched a bit and got to this: Scala 
> for-comprehension to Clojure 
> <http://stackoverflow.com/questions/25655132/how-to-convert-this-map-flatmap-into-a-for-comprehension-in-clojure>
>
> But as you can see that is not really about having kind of generator 
> function which is wrapped into flatMap
> call. Perhaps this is not in Clojure's spirit at all, perhaps there is 
> some Clojure idiom to achieve something similar.
>
> Any tip, suggestion, critic is welcome and appreciated.
>
> If someone is wondering how in the world I came up to this, I am doing 
> some f-p exercises which I've got Scala solution for
> however I am not very interested in Scala and I am doing Clojure so I just 
> need to grasp concepts...
>
> Thanks in advance...
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Scala for-comprehension to Clojure

2015-11-26 Thread Rastko Soskic
Hi, 
I am aware of philosophical differences of Scala and Clojure
but functional programming should be a pretty common ground :)
Thus I need help, I am trying to mimic Scala's for comprehension in Clojure.

Hopefully someone will be able to aid me with the following (perhaps more 
familiar with Scala):

Scala for comprehension is errr how to say "de-sugared" to series 
of map and flatMap calls... thus, it is possible to use plain functions in 
for-comprehension
like:
*val *ns: Conv[List[Int]] = *for *{ // Conv is just alias for functions of 
type B => (B, A)
x <- int // int is function again of type Conv
y <- int
xs <- ints(x) // this is just sequence of numbers
} *yield *xs.map(_ * y)

I don't need all the nuts and bolts, just some guideline for achieving 
something similar
in Clojure.

I am not lazy :) I've already eagerly researched a bit and got to this: Scala 
for-comprehension to Clojure 
<http://stackoverflow.com/questions/25655132/how-to-convert-this-map-flatmap-into-a-for-comprehension-in-clojure>

But as you can see that is not really about having kind of generator 
function which is wrapped into flatMap
call. Perhaps this is not in Clojure's spirit at all, perhaps there is some 
Clojure idiom to achieve something similar.

Any tip, suggestion, critic is welcome and appreciated.

If someone is wondering how in the world I came up to this, I am doing some 
f-p exercises which I've got Scala solution for
however I am not very interested in Scala and I am doing Clojure so I just 
need to grasp concepts...

Thanks in advance...

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.