fpinscala | go along with the book `` Functional Programming | Learning library
kandi X-RAY | fpinscala Summary
kandi X-RAY | fpinscala Summary
This repository contains exercises, hints, and answers for the book Functional Programming in Scala. Along with the book itself, it's the closest you'll get to having your own private functional programming tutor without actually having one.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of fpinscala
fpinscala Key Features
fpinscala Examples and Code Snippets
Community Discussions
Trending Discussions on fpinscala
QUESTION
I implemented a fold
method in my scala code. I can use it to determine the size
and the depth
of the tree.
Now I'd like to implement the methods max
and map
which should work like here.
The difference is, that the value is saved in the branch instead of the leaf.
Here's my code so far:
ANSWER
Answered 2020-May-07 at 08:28Firstly, you can put fold
to companion object of Tree
(since fold
doesn't use this
i.e. is "static").
Secondly, start with implementing map
for your case
QUESTION
I'm following a book's example to implement a Steam class using lazy evaluation in Scala.
...ANSWER
Answered 2017-Jul-22 at 20:06No. If you put a breakpoint on the
println
you'll find that the method is actually being called when you first create theStream
. The lineStream(printAndReturn, ...
actually calls your method however many times you put it there. Why? Consider the type signatures forcons
andapply
:
QUESTION
I'm learning Scala and in a book that I'm reading (Functional Programming in Scala) I came across an example of a custom List
implementation in Scala which goes like this:
ANSWER
Answered 2020-Feb-10 at 08:26I note that your Cons
class already has a public tail
member. I'd be tempted to start there and just make it universal...
QUESTION
ANSWER
Answered 2019-Oct-28 at 11:35As @jwvh already pointed out, get
is a method defined in object State
.
The get
-method is neither applied to any Machine
nor is a Machine
used as parameter, because the function has the following signature:
QUESTION
I am having problems understanding this code from the Book FP in Scala. Here is the code:
...ANSWER
Answered 2019-Apr-24 at 13:13Well, you seem to be mostly comprehensive to me. Nevertheless, I would clarify some points:
- I'd rather say "so I suppose
f.curried
is working becauseA => (B => B)
is the same as(A => B => B)
" (it is ambiguous here and you're talking aboutf.curried
result type basically, not withz
) - I'd rather put a point instead of a comma here: "
foldMap
is expecting a function f: A => B . In foldRight, ... " and pretty much every where else. Shorter phrases, clearer explanation. - what could be an error, (and what is confusing to you?) is that
(f.curried)(z)
doesn't work on its own and is not called afterfoldMap(as, endoMonoid[B])
. It's firstfoldMap(as, endoMonoid[B])(f.curried)
which is called and then(z)
. The first returnsB => B
and called with the second returnsB
.
QUESTION
In Functional Programming in Scala, the author asks to express FoldRight via FoldLeft. And then the author offers the following implementation:
...ANSWER
Answered 2019-Mar-10 at 08:33You've written an implementation that has the same signature as foldRight
, but it doesn't have the right semantics when the combination operation isn't commutative. To take one example, a right fold with the empty list as zero and cons as the combination operation should be identity:
QUESTION
I came across the following scenario when studying the book "Functional Programming in Scala" by Paul Chiusano and Runar Bjanarson (Ch. 7 - Purely functional parallelism).
...ANSWER
Answered 2019-Feb-01 at 01:12OK, after a long investigation I believe I have an answer. The full story is long but I'll try to shorten it by simplifying and avoiding many details.
Note: Potentially Scala can be compiled to various different target platforms but this particular issue happened on the Java/JVM as the target so this is what this answer is about.
The deadlock you see has nothing to do with the size of the thread pool. Actually it is the outer fork
call that hangs. It is related to a combination of REPL implementation details and multi-threading but it takes learning a few pieces to understand how it happens:
- how Scala REPL works
- how Scala compiles
object
s to Java/JVM - how Scala emulates the by-name parameters on Java/JVM
- how Java/JVM runs the static initializers of classes
A short(er) version (see also Summary at the end) is that this code hangs under a REPL because when it is being executed by the REPL, it is logically similar to the following code:
QUESTION
I came across this function definition when studying the book "Functional Programming in Scala" by Paul Chiusano and Runar Bjanarson (Ch. 7 - Purely functional parallelism):
...ANSWER
Answered 2019-Jan-27 at 17:25supply a function as a lazy argument
For the fork
function has a lazy argument a: => Par[A]
, that's means a
will not eval when fork(a)
until it's submitted to ExecutorService
with a Callable
, since a
's response type also Par[A]
, it will also asynchronously eval again, and also can recursively fork execute with the same ExecutorService.
So for my understanding, the fork
with lazy argument a: => Par[A]
, it will have the ability of parallel and recursively fork tasks.
In Scala, lazy function: it means lazy evaluation until invoke this function. for your example, you are declaring a lazy function a: => Par[A]
, so when you call fork
method like: fork(myFunction())
, this will not evaluate myFunction
immediately, it only will eval when a(es).get
executed.
Example:
QUESTION
I am having trouble understanding the following implementation of the scanRight
function in Scala.
ANSWER
Answered 2019-Jan-08 at 08:35The tuple is the argument to the foldRight
call: (z, Stream(z))
. The first value in the tuple is the result of the scan so far. The second value is the Stream
that will be the eventual result of the scanRight
call.
Each pass of the fold
updates the result of the scan by calling f
on the current value in the sequence (a
) and the previous scan value (the first element in the tuple). The result is added to the stream (in the second element of the tuple) using cons
. Both values are passed on to the next iteration of the fold
as a new tuple.
When the fold
finishes it returns the tuple, but only the second element is required by scanRight
so is extracted from that tuple (._2
) and returned.
QUESTION
I am looking at exercise 5.8 in book "FP in Scala" and the question is:
"Generalize ones slightly to the function constant, which returns an infinite Stream of a given value."
...ANSWER
Answered 2019-Jan-05 at 13:28I think it is because with the lazy implementation, you are creating the object only once, and memoizing it, so when you call constant
, you are referring to the same object over and over again, something like this:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install fpinscala
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page