Questions tagged [scalaz]
Scalaz provides type classes and purely functional data structures for Scala
981 questions
0
votes
0answers
9 views
Iterate over scalaz lens that returns a map
I'm struggling with combining state, lenses and maps.
Given the following case classes
case class Item(id: Int, text: String)
case class Container(items: Map[Int, Item])
case class Parent(...
0
votes
1answer
28 views
In ZIO, is there a way to transform an IO[Nothing, T] to T, and if not, why not?
At some point, after having dealt with errors and having converted IO[E, T] to an IO[Nothing, T], it seems that we have made it sufficiently explicit that we may directly refer to the value as being ...
2
votes
1answer
53 views
Safe Try with clean up
I've been trying to find a nice way to combine a chain of Try in Scala with subsequent resources clean up. What I ended up with looked something like this (powered by scalaz)
for {
x <- \/....
1
vote
1answer
42 views
Close InputStream wrapped into IO
I'm using IO (cats/scalaz does not matter). And I want to use bracket to close InputStream after I'm done with it. The problem is that I'm reading gzipped files. Here is what I tried:
I (Incorrect).
...
0
votes
2answers
40 views
How to replace pound sign £ in scala
In sales column i have values with pound sign £1200. It is not readable by Data frame in scala, please help me for the same. i want column value in double, 1200. I am using below method but its not ...
1
vote
2answers
56 views
Avoiding boilerplate with OptionT (natural transform?)
I have the following methods:
trait Tr[F[_]]{
def getSet(): F[Set[String]]
def checksum(): F[Long]
def value(): F[String]
def doRun(v: String, c: Long, s: Set[String]): F[Unit]
}
...
0
votes
1answer
22 views
Extract value from AsyncEither - ScalaZ
I am new in ScalaZ and in Scala in general so I faced the issue below.
I wrote some unit tests for my application and what I need is to extract the value of the actual response.
May you have any ...
2
votes
0answers
45 views
WriterT or ReaderWriterStateT?
I'm trying to apply WriterT to my case and faced some problem. I have the following writer according to my needs:
val writer: WriterT[Option, List[IO[Unit]], Set[String]] = //
And this is pretty ...
0
votes
2answers
68 views
Replace elements in List by condition
I have a pretty much large val s: List[Int] = //..., a function f: Int => Boolean and a function transform: Int => Int.
The problem: I want to create another List[Int] such that all elements e: ...
3
votes
1answer
66 views
What is Store in scalaz
I'm trying to understand Lenses in scalaz (surprisingly didn't find something similar in cats-core) and I came across the so called Store which is a type alias:
type StoreT[F[_], A, B] = ...
3
votes
1answer
65 views
How to correctly write a foldLeft function that may fail?
Consider the following snippet
def sayManyTimes(a: String): IO[String] = IO(a * 3)
(1 to 2).foldLeft("Read this: ")((c, i) =>
c + sayManyTimes("mayo").unsafeRunSync)
Now, this achieves ...
6
votes
2answers
128 views
Scalaz, the purpose of *syntax classes
In scalaz when we define a module, we additionally define implicit, helper functions. Here is an example of definition and how it could be used by a client:
trait Functor[F[_]] {
def map[A,B](fa: F[...
1
vote
0answers
47 views
Where to put implicits and helper functions, traits vs companion objects
I first describe way of thinking, and then my questions. I use Functor definition as an example. It is defined in scalaz, so I use it as an example with small changes to make it less complex.
Here is ...
3
votes
0answers
61 views
State transition with `State`
I'm trying to design state transition using State monad. I'm actually have a java.nio.ReadableByteChannel and I want to read it by fixed size chunks (e.g. 4096 bytes) and then combine some record from ...
2
votes
1answer
51 views
Designing referentially transparent function for reading from channel
I'm trying to adhere to pure-FP style and want to design a referentially transparent function.
I have a java.nio.channels.SeekableByteChannel, which is a data source. And soon as I open a file and ...
1
vote
1answer
33 views
Why can't we provide implicit typeclass when a type is covariant?
I'm designing my algebraic data type and faced a problem of unabling to provide implicit typeclass. Here is what I have:
sealed abstract class Stream[+F[_], +T] {
def run()(implicit M: Monad[F]): F[...
1
vote
0answers
53 views
How to make effectful computation referential transparent
I'm learning FP by writing simple apps. And now I'm approaching effect monads (cats.effect.IO/scalaz.IO does not really matter). I have two functions:
def open(path: String): IO[InputStream] = IO {
...
0
votes
0answers
35 views
Is it appropriate to perfrom IO::unsafeRunSync on the same instance multiple times?
I have a question about IO monad. I opened some InputStream and want to use IO to read from it. Here is the example:
def read(io: IO[Option[Array[Byte]]]): IO[Unit] = IO {
io.unsafeRunSync() match {...
0
votes
1answer
50 views
Scalaz ValidationNel - folding
I've recently used ScalaZ for validation purposes and decided to choose ValidationNel as a fail fast behavior wasn't desired. I had more than 12 validation checks to make, thus I couldn't use operator ...
1
vote
1answer
44 views
How to stop state transition when state satisfies some condition?
I'm quite new to scalaz/cats and have a question about State monad (cats or scalaz does not matter). Consider the following well-known example with Stack:
object StateTest {
type Stack = List[Int]
...
1
vote
1answer
45 views
Effects to use for wrapping impure methods?
I'm trying to understand how to use effect monads (cats.effect.IO or scalaz.IO does not matter). Imagine I have the following method:
def extract(str: String): String = {
if(str.contains("123"))
...
4
votes
1answer
75 views
Monad transformer for Future[Either[Error, Option[User]]]
Consider the signature of retrieveUser where retrieving a non-existent user is not modelled as an error, that is, it is modelled as Future[Right[None]]:
def retrieveUser(email: String): Future[Either[...
1
vote
2answers
45 views
Case class with Id with reactivemongo, should be optional or required
I'm building the following api for Users in play scala:
case class User(_id: BSONObjectId, username: String)
The issue is that when the client sends a request in order to store/create a new user the ...
5
votes
1answer
65 views
Why would validation break the monad laws?
On SO an explanation is given why a Validation like in scalaz, cats (Scala), or Arrow (Kotlin) can't be a monad.
As far as I understand it's because they've modelled monads in terms of applicative ...
2
votes
3answers
50 views
Scala chain of transformations stopping if error
I want to apply a sequence of transformations to a String but stopping when there is an error. This is an example of a more general pattern (a kind of Chain of Responsibility pattern or Visitor ...
0
votes
0answers
38 views
scalaz stream: parallel multi sink
I just started using scalaz-stream library and I'd like to implement the following scenario. I have a component which reads some events from a source stream (scalaz queue), mutates it's state and ...
0
votes
0answers
23 views
Monad IO catchAll operator behave
I´m learning the monad IO of scalaZ and I cannot understand how catchAll and catchSome operators works.
I was expecting so see a behave like the onError or onErrorrResumeNext of RxJava, but instead is ...
1
vote
1answer
67 views
Deal with Either as a fs.Stream
If I have a fs2.StreamApp[IO], how do I best deal with Either (or \/) when constructing the main app's stream?
For example, I build up some inputs to the program as an either (as I like the API -- it'...
-1
votes
1answer
98 views
Ideal chunk in scala fs2 stream performance gain in production
was wondering if the increase in chunk size in scala fs2 stream will give the performance gain?
import cats.effect.{IO, Sync}
import fs2.{io, text}
import java.nio.file.Paths
def ...
0
votes
2answers
32 views
Service class and logging design Scala with Futures
I have a service class that gets some data from a database (for context, I'm using Play! Framework). Here's an example method:
def getAccessToken(id: BSONObjectID): Future[Option[String]] = {
...
0
votes
1answer
29 views
Scala : EndoMonoids Function composition and Associativity Rules
I was going through the laws that govern Monoids and one of the two laws state that the append operation must be associative. For function composition this means that for all functions X->X given ...
1
vote
1answer
54 views
Coproduct with multiple values
I´m playing with Coproduct of shapeless but I dont know if I´m using wrong but I dont know how can I create a coProduct with mutlple values
Having this code
case class Name(value: String)
case ...
1
vote
2answers
62 views
How to define a recursive algebra for use in free applicative?
I'm facing the problem of defining a recursive algebra to use with FreeApplicative.
Here is my failed attempt.
Let's assume we want the functionality to tag (groups of) effectful values.
I have ...
1
vote
2answers
43 views
Multiple validations on single object?
scalaz Validations have +++ which accumlates both errors and successes. However my success type isn't a F[T] with Semigroup[F], it's just T (unless I use the Id semigroup...). Basically I want to ...
0
votes
1answer
31 views
scalaz - using if within for comprehension
I'm trying to achieve the following using EitherT:
def op1 : EitherT[Future, String, Int] = ???
def op2 : EitherT[Future, String, Int] = ???
for {
value1 <- op1
if value1 > 20
value2 &...
1
vote
1answer
27 views
Scalaz Validation disregard Apply method
(List("ha", "heh", "hmm") |@| List("?", "!", ".")) {_ +"doeswork"+ _}
returns correctly
res0: List[String] = List(hadoeswork?, hadoeswork!, hadoeswork., hehdoeswork?, hehdoeswork!, hehdoeswork....
1
vote
1answer
80 views
spark dataframe matching nth regex pattern
How can I scan a spark dataframe string column for regex pattern and get nth match (i.e. USD 200,765.00)? BTW, I tried adding "{3}" at the end of the pattern and did not work.
I appreciate any ...
0
votes
3answers
59 views
Scala: Append only Some's to immutable list
So say we're given a List[String] and bunch of Option[String]'s call them a, b, c. Say I want to append the valid (Some's) Options[String]'s out of a, b, c to my existingList[String]. What would be ...
1
vote
1answer
52 views
Lifting a function to ~> in scalaz
I have the following types and declarations:
import scalaz._, Scalaz._
trait Container[T]
type FreeContainer[A] = Free[Container, A]
type FreeFreeContainer[A] = Free[FreeContainer, A]
val fc: ...
8
votes
1answer
116 views
Interpreting a list of free monads vs. interpreting a free monad of a list
I'm learning functional programming and have some (maybe obvious, but not for me :) ) question about monad. Every monad is an applicative functor. Applicative functor in turn can be defined as a ...
1
vote
1answer
46 views
Can we reverse Applicative of List?
I'm reading about scalaz and noticed that we can make a list of Applicatives to be an Applicative of List.
def sequenceA[F[_]: Applicative, A](list: List[F[A]]): F[List[A]] = list match {
...
1
vote
1answer
56 views
Understanding Free monad in scalaz
I'm experimenting with Free monad in Scalaz and trying to build simple interpreter to parse and evaluate expressions like:
dec(inc(dec(dec(10)))
where dec means decrement, inc means increment. Here ...
0
votes
1answer
57 views
How to accept all types except Future in definition of method in Scala?
def withCachedFuture[K, V](key: K)(future: ⇒ Future[V])(implicit cache: Cache[K, Future[V]], ec: ExecutionContext): Future[V] = {
Option(cache getIfPresent key) match {
case Some(result) ⇒
...
2
votes
1answer
101 views
What is functor bias?
Following up from C++ Functors - and their uses, I have come across 'right biased functor' and 'left biased functor'. I have tried to do some research on my own, but I am still failing to understand ...
1
vote
1answer
47 views
Scalaz Task not starting
I'm trying to make an asyncronous call using scalaz Task.
For some strange reason whenever I attempt to call methodA, although I get a scalaz.concurrent.Task returned I never see the 2nd print ...
3
votes
0answers
54 views
Why is Apply[F[_]] So Slow?
For the purposes of a unit test, I needed to zip the contents of 5 lists together. I had one test case that was never completing. I managed to track it down to the use of Apply[List].apply5 from the ...
1
vote
1answer
61 views
Scala & ScalaTest operator === conflict
I want to test operator === of Scalaz using ScalaTest.
I have written simply test:
class ComparisonTest extends FunSuite {
test("=== operator of Scalaz") {
assert(1 === 1) // i want to check/...
1
vote
1answer
92 views
Scalaz: How to use EitherInstances with MonadError?
I want to use scala.util.Either together with scalaz.MonadError, but I'm running into some type issues.
My simplest failing code is below:
object Foo extends EitherInstances {
private val success: ...
1
vote
1answer
90 views
Foldable “foldMap” that take a partial function: foldCollect?
Say, I have the following object:
case class MyFancyObject(a: String, b: Int, c : Vector[String])
And what I needed is to get a single Vector[String] containing all 'c's that match a given partial ...
0
votes
1answer
24 views
ValidationNel Error after upgrading Scalaz to 7.2
I'm running into validation issues after upgrading the Scalaz version to 7.2. The following code was working in prior Scalaz version.
def registerOrUpdate(enc: EncAdt, dx: List[DiagnosisAdt], provs: ...