문제

This question isn't meant as flame-bait! As it might be apparent, I've been looking at Scalaz recently. I'm trying to understand why I need some of the functionality that the library provides. Here's something:

import scalaz._
import Scalaz._
type NEL[A] = NonEmptyList[A]
val NEL = NonEmptyList

I put some println statements in my functions to see what was going on (aside: what would I have done if I was trying to avoid side effects like that?). My functions are:

val f: NEL[Int] => String    = (l: NEL[Int]) => {println("f: " + l); l.toString |+| "X" }
val g: NEL[String] => BigInt = (l: NEL[String]) => {println("g: " + l);  BigInt(l.map(_.length).sum) }

Then I combine them via a cokleisli and pass in a NEL[Int]

val k = cokleisli(f) =>= cokleisli(g)
println("RES: "  + k( NEL(1, 2, 3) ))

What does this print?

f: NonEmptyList(1, 2, 3)
f: NonEmptyList(2, 3)
f: NonEmptyList(3)
g: NonEmptyList(NonEmptyList(1, 2, 3)X, NonEmptyList(2, 3)X, NonEmptyList(3)X)
RES: 57

The RES value is the character count of the (String) elements in the final NEL. Two things occur to me:

  1. How could I have known that my NEL was going to be reduced in this manner from the method signatures involved? (I wasn't expecting the result at all)
  2. What is the point of this? Can a reasonably simple and easy-to-follow use case be distilled for me?

This question is a thinly-veiled plea for some lovely person like retronym to explain how this powerful library actually works.

도움이 되었습니까?

해결책

이를 수행하는 "올바른"방법은 입력 된 것처럼 데이터를 수집하는 것입니다!

테이블 뷰 셀이 화면이 꺼져 있으면 테이블 뷰 셀이 화면에있는 경우에는 단순히 작동하지 않을 것입니다.(그리고 아직 존재하지 않을 수도 있습니다!).

다른 팁

Cojoin is also defined for scalaz.Tree and scalaz.TreeLoc. This can be exploited to find a stream of all paths from the root of the tree to each leaf node.

def leafPaths[T](tree: Tree[T]): Stream[Stream[T]]
  = tree.loc.cojoin.toTree.flatten.filter(_.isLeaf).map(_.path)

Using coKleisli arrow composition, we can do this, for example:

def leafDist[A] = (cokleisli(leafPaths[A]) &&& cokleisli(_.rootLabel))
  =>= (_.map(s => (s._2, s._1.map(_.length).max)))

leafDist takes a Tree and returns a copy of it with each node annotated with its maximum distance from a leaf.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top