scalaz

How to combine Option values in Scala?

故事扮演 提交于 2019-11-30 06:55:52
问题 I want to be able to apply an operation f: (T,T) => T to Option[T] values in Scala. I want the result to be None if any of the two values is None . More specifically, I want to know if there is a shorter way to do the following: def opt_apply[T](f: (T,T) => T, x: Option[T], y: Option[T]): Option[T] = { (x,y) match { case (Some(u),Some(v)) => Some(f(u,v)) case _ => None } } I have tryied (x zip y) map {case (u,v) => f(u,v)} but the result is an Iterator[T] not an Option[T] . 回答1: scala> val (x

Clojure's 'let' equivalent in Scala

社会主义新天地 提交于 2019-11-30 06:38:51
Often I face following situation: suppose I have these three functions def firstFn: Int = ... def secondFn(b: Int): Long = ... def thirdFn(x: Int, y: Long, z: Long): Long = ... and I also have calculate function. My first approach can look like this: def calculate(a: Long) = thirdFn(firstFn, secondFn(firstFn), secondFn(firstFn) + a) It looks beautiful and without any curly brackets - just one expression. But it's not optimal, so I end up with this code: def calculate(a: Long) = { val first = firstFn val second = secondFn(first) thirdFn(first, second, second + a) } Now it's several expressions

Typeclasses and inheritance in scalaz

て烟熏妆下的殇ゞ 提交于 2019-11-30 05:42:25
问题 This is my second try to define the problem, I can't get my head around it. I want to be able to define an algebraic type and define a simple typeclass over it, let's say Show . In haskell I do: data Tree a = EmptyTree | Node a deriving (Show) Now, if I type EmptyTree - haskell can show it, so it belongs to Show . Now I am trying to do the same in scala: sealed abstract class Tree[+T] case object EmptyTree extends Tree[Nothing] case class Node[T](value: T) extends Tree[T] Then I define Show

How do I get the scalaz IDEA live templates working for the symbolic methods?

老子叫甜甜 提交于 2019-11-30 05:09:35
Many of the methods in scalaz have symbolic unicode equivalents, such as forever and ∞ (of course, I have this the wrong way round, the symbolic methods really have ASCII equivalents). The project contains a live templates XML file for IDEA so these can be auto-completed , I believe by using the forever+TAB shortcut (in the above instance). I can't figure out how to import this live template into IDEA and actually use it, though. How can I do that? CrazyCoder Live Templates XML file should be placed under IDEA configuration directory, templates subdirectory. IDEA configuration folder location

Will using Scala in a more functional way (scalaz) incur a performance/maintainability penalty?

試著忘記壹切 提交于 2019-11-30 03:28:03
I'm currently working on a small project (< 10k loc) which is mainly pure but relies on mutable optimizations mainly based on iterators and some data-structure reuse for heavy-duty calculations. I'd like to learn a bit more functional programming and want to get more type safety, by e.g. wrapping mutable computations into state transformer monads and the like. For this purpose there exists the scalaz library. Question One When abstracting my computations on a larger scale by using all the fancy functional stuff, will I introduce performance killers that I won't get rid of? Like when my

How to handle `Reader` monad and `Try`?

旧时模样 提交于 2019-11-29 23:19:15
I'm reading this great article about dependency injection in scala with Reader monad . The original example is working well, but I did a little bit change on the return types of the UserRepository.get/find . It was User , but I changed it to Try[User] . Then the code won't be compiled, I had tries many times, but still without lucky. import scala.util.Try import scalaz.Reader case class User(email: String, supervisorId: Int, firstName: String, lastName: String) trait UserRepository { def get(id: Int): Try[User] def find(username: String): Try[User] } trait Users { def getUser(id: Int) = Reader

Scalaz Validation with applicative functor |@| not working

℡╲_俬逩灬. 提交于 2019-11-29 21:21:23
问题 I'm trying to use Scalaz 7 Validation in my app. However, I'm having an issue getting the |@| applicative functor to coalesce my failures. Here's the code I have: type ValidationResult = ValidationNel[String, Unit] def validate[A: ClassTag](instance: A, fieldNames: Option[Seq[String]] = None): ValidationResult = { val fields = classTag[A].runtimeClass.getDeclaredFields val fieldSubset = fieldNames match { case Some(names) => fields.filter { field => names.contains(field.getName) } case None =

Functor Instance for Type Constructor with Two Parameters in Scala

主宰稳场 提交于 2019-11-29 21:01:41
问题 I have a class Foo with two parameters, and I am trying to write a Functor instance for Foo with the first parameter fixed, as follows: object Scratchpad { trait Functor[F[_]] { def fmap[A, B](f: A => B): F[A] => F[B] } case class Foo[X, Y](value: Y) implicit def fooInstances[X]: Functor[Foo[X, _]] = new Functor[Foo[X, _]] { def fmap[A, B](f: A => B): Foo[X, A] => Foo[X, B] = foo => Foo[X, B](f(foo.value)) } } But the above code fails to compile, generating the following error: Error:(9, 41)

Finding my way through Scalaz [duplicate]

喜欢而已 提交于 2019-11-29 20:11:49
Possible Duplicate: Good scalaz introduction I would like to learn more about Scalaz, possibly using Scalaz7 to be avoid rewiring my brain once it is declared stable. My problem is that Scalaz contains a lot of functionality. While most of it is meant to be independent of other parts, I would like to have a bird's eye view of the global funcitonality offered by Scalaz and how it is organized. As far as I know, Scalaz offers, among other things, Functor , Applicative and Monad traits, new monads such as Validation (edit: turns out it is only an applicative) monad transformers ( OptionT ,

Function syntax puzzler in scalaz

喜你入骨 提交于 2019-11-29 18:43:46
Following watching Nick Partidge's presentation on deriving scalaz , I got to looking at this example, which is just awesome: import scalaz._ import Scalaz._ def even(x: Int) : Validation[NonEmptyList[String], Int] = if (x % 2 ==0) x.success else "not even: %d".format(x).wrapNel.fail println( even(3) <|*|> even(5) ) //prints: Failure(NonEmptyList(not even: 3, not even: 5)) I was trying to understand what the <|*|> method was doing, here is the source code: def <|*|>[B](b: M[B])(implicit t: Functor[M], a: Apply[M]): M[(A, B)] = <**>(b, (_: A, _: B)) OK, that is fairly confusing (!) - but it