Commit fcad50b3 authored by Tim Hegemann's avatar Tim Hegemann
Browse files

add task sheet 2

parents
MIT License
Copyright (c) 2019 Lehrstuhl für Informatik VI, Uni Würzburg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This repository contains exercises adapted from "Functional Programming in
Scala", Copyright (c) 2012, Manning Publications, Co., also published under the
MIT license.
\ No newline at end of file
# tasksheets
Source templates for task sheets in Exercises for Introduction to Functional Programming.
## Overview:
Templates are within `src/main/scala`. Parts that you should complete are
usually marked with `???`.
| task sheet | package |
|---------------------------------|-------------------------------------------------------------------|
| 1: Functional Data Structures | <no templates> |
| 2: Error Handling | [`fp02`](src/main/scala/fp02) |
## Usage tips:
To keep your local solutions to the exercises when pulling from the repository,
use
```shell
git pull --rebase --autostash
```
This will keep both commited and uncommited changes.
If you are using Intellij IDEA, "rebase" is available as option in the update
dialog and stashing is default.
name := "tasksheets"
organization := "de.uniwue.fp"
version := "0.1-SNAPSHOT"
scalaVersion := "2.12.8"
scalacOptions ++= Seq(
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-unchecked",
"-Xfatal-warnings",
"-Xlint",
"-Yno-adapted-args",
"-Ywarn-dead-code",
"-Ywarn-numeric-widen",
"-Ywarn-value-discard",
"-Ypartial-unification",
"-Xfuture",
"-Ywarn-unused-import",
"-Ywarn-unused:implicits",
"-Ywarn-unused:locals",
"-Ywarn-unused:params",
"-Ywarn-unused:patvars",
"-Ywarn-unused:privates",
"-Ypatmat-exhaust-depth", "40"
)
resolvers += Resolver.sonatypeRepo("releases")
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.3")
// Disallow some language construcs
// your bonus exercises will have to compile with these options
addCompilerPlugin("org.wartremover" %% "wartremover" % "2.4.1")
scalacOptions ++= Seq(
"-P:wartremover:traverser:org.wartremover.warts.AsInstanceOf",
"-P:wartremover:traverser:org.wartremover.warts.IsInstanceOf",
"-P:wartremover:traverser:org.wartremover.warts.MutableDataStructures",
"-P:wartremover:traverser:org.wartremover.warts.Null",
"-P:wartremover:traverser:org.wartremover.warts.Return",
"-P:wartremover:traverser:org.wartremover.warts.Throw",
"-P:wartremover:traverser:org.wartremover.warts.Var",
"-P:wartremover:traverser:org.wartremover.warts.While",
)
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.5"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"
package fp02
object FP02 {
// Known from lecture
def mean(l: List[Double]): Option[Double] = if (l.isEmpty) None else Some(l.sum / l.size)
// 1
def variance(l: List[Double]): Option[Double] = ???
// 2a
def sequenceViaFold[A](l: List[Option[A]]): Option[List[A]] = ???
// 2b
def traverseViaPatternMatching[A,B](l: List[A])(f: A => Option[B]): Option[List[B]] = ???
// 2c
def traverseViaFold[A,B](l: List[A])(f: A => Option[B]): Option[List[B]] = ???
// 2d
def sequenceViaTraverse[A](l: List[Option[A]]): Option[List[A]] = ???
// 3a
def sequence[E,A](es: List[Either[E, A]]): Either[E, List[A]] = ???
def traverseViaSequence[E,A,B](l: List[A])(f: A => Either[E, B]): Either[E, List[B]] = ???
// 3b
def traverse[E,A,B](l: List[A])(f: A => Either[E, B]): Either[E, List[B]] = ???
def sequenceViaTraverse[E,A](l: List[Either[E, A]]): Either[E, List[A]] = ???
/// HERE BE DRAGONS -- DO NOT READ AHEAD!
implicit class Map2ForOption[A](o: Option[A]) {
def map2[B,R](p: Option[B])(f: (A,B) => R): Option[R] = for {
a <- o
b <- p
} yield f(a,b)
}
implicit class Map2ForList[A](o: List[A]) {
def map2[B,R](p: List[B])(f: (A,B) => R): List[R] = for {
a <- o
b <- p
} yield f(a,b)
}
implicit class Map2ForEither[A,E](o: Either[E,A]) {
def map2[B,R](p: Either[E,B])(f: (A,B) => R): Either[E,R] = for {
a <- o
b <- p
} yield f(a,b)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment