Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2nd task done #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 67 additions & 6 deletions 02/practice/src/main/scala/org/spbsu/mkn/scala/IntList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,81 @@ package org.spbsu.mkn.scala

import org.spbsu.mkn.scala.IntList._

import scala.annotation.tailrec

sealed trait IntList {
def head: Int

def tail: IntList

def drop(n: Int): IntList

def take(n: Int): IntList

def map(f: Int => Int): IntList
def ::(elem: Int): IntList = ???

def ::(elem: Int): IntList = IntCons(elem, this)

}

object IntList {
def undef: Nothing = throw new UnsupportedOperationException("operation is undefined")
def fromSeq(seq: Seq[Int]): IntList = ???
def sum(intList: IntList): Int = ???
def size(intList: IntList): Int = ???
// extra task: implement sum using foldLeft
// def foldLeft(???)(???): ??? = ???

@tailrec
def foldLeft(f: Int => Int => Int)(intList: IntList)(init: Int): Int = intList match {
case IntNil => init
case _ => foldLeft(f)(intList.tail)(f(intList.head)(init))
}

def fromSeq(seq: Seq[Int]): IntList = seq match {
case _ if seq.isEmpty => IntNil
case _ => seq.foldRight(IntNil: IntList)((elem: Int, intList: IntList) => IntCons(elem, intList))
}

def sum(intList: IntList): Int = intList match {
case IntNil => undef
case _ => foldLeft(x => _ + x)(intList)(0)
}

def size(intList: IntList): Int = intList match {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно использовать foldLeft

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fuCtor, исправила

case IntNil => 0
case _ => 1 + size(intList.tail)
}

}

case object IntNil extends IntList {
override def head: Int = undef

override def tail: IntList = undef

override def drop(n: Int): IntList = n match {
case 0 => this
case _ => undef
}

override def take(n: Int): IntList = n match {
case 0 => this
case _ => undef
}

override def map(f: Int => Int): IntList = this
}

case class IntCons(override val head: Int, override val tail: IntList) extends IntList {
override def drop(n: Int): IntList = n match {
case 0 => this
case _ => tail.drop(n - 1)
}

override def take(n: Int): IntList = n match {
case 0 => IntNil
case _ => IntCons(head, tail.take(n - 1))

}

override def map(f: Int => Int): IntList = IntCons(f(head), tail.map(f))


}