home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (IsomorphismUsage.scala)

This example Scala source code file (IsomorphismUsage.scala) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

app, isofunctortemplate, isomorphismmonad, isomorphismmonoid, isomorphismtraverse, isomorphismusage, isoset, list, seq

The IsomorphismUsage.scala Scala example source code

package scalaz.example

import scalaz._, Isomorphism._
import std.list._
import std.anyVal._
import syntax.monoid._
import syntax.monad._
import syntax.equal._
import syntax.traverse._

object IsomorphismUsage extends App {
  def isoSet[A] = new IsoSet[Seq[A], List[A]] {
    def to: Seq[A] => List[A] = _.toList
    def from: List[A] => Seq[A] = _.toSeq
  }
  def isoFunctor = new IsoFunctorTemplate[Seq, List] {
    def to[A](sa: Seq[A]): List[A] = sa.toList
    def from[A](la: List[A]): Seq[A] = la.toSeq
  }

  implicit def isoMonoid[A]: IsomorphismMonoid[Seq[A], List[A]] = new IsomorphismMonoid[Seq[A], List[A]] {
    def G = Monoid[List[A]]
    def iso = isoSet
  }
  implicit val isoMonad: IsomorphismMonad[Seq, List] = new IsomorphismMonad[Seq, List] {
    def G = Monad[List]
    def iso = isoFunctor
  }
  implicit val isoTraverse: IsomorphismTraverse[Seq, List] = new IsomorphismTraverse[Seq, List] {
    def G = Traverse[List]
    def iso = isoFunctor
  }

  assert((Seq(1, 2) |+| Seq(3, 4)).toList === List(1, 2, 3, 4))
  assert {
    val seq = Seq(1, 2, 3) >>= {x => Seq(x, x+1)}
    val lst = List(1, 2, 3) >>= {x => List(x, x+1)}
    seq.toList === lst
  }
  assert(List(Seq(1, 2), Seq(3, 4)).sequence.toList === List(List(1, 2), List(3, 4)).sequence)
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala IsomorphismUsage.scala source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.