Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • joel.cavat/scala2020
  • alexandr.favre1/scala2020
  • alexandr.arondel/scala2020
  • joel.vonderwe/scala2020
4 results
Select Git revision
Show changes
Commits on Source (5)
Showing
with 418 additions and 0 deletions
../.idea/
target/
.idea/
No preview for this file type
No preview for this file type
name := "TP2"
version := "0.1"
scalaVersion := "2.13.1"
scalaSource in Compile := baseDirectory.value / "src"
javaSource in Compile := baseDirectory.value / "java" / "src"
scalaSource in Test := baseDirectory.value / "test"
sbt.version = 1.3.7
\ No newline at end of file
package ch.hepia.tp2
import java.util._
class Stack[A] {
private val content: List[A] = new ArrayList[A]
//Return true if Stack is Empty
def isEmpty: Boolean = content.isEmpty
//return size of stack
def size: Int = content.size
//stack an element on the stack
def push(a: A): Unit = content.add(0, a)
//Unstack an element and return it
def pop: A = {
if(this.size == 0 ) {
throw new NoSuchElementException
}else{
content.remove(0)
}
}
//Swap the 2 first elements of the stack, do nothing if less than 1 element
def swap: Unit = {
if(this.size > 1) {
this.push(content.remove(1))
}
}
override def toString: String = {
val sb: StringBuilder = new StringBuilder
sb.append("[")
this.content.forEach((e) => sb.append(e).append(", "))
sb.append("]")
sb.toString
}
}
name := "TP3"
version := "0.1"
scalaVersion := "2.13.1"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.1.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.0" % "test"
\ No newline at end of file
sbt.version = 1.3.7
\ No newline at end of file
/*
* Implémenter les fonctions suivantes en suivant les commentaires.
* Respectez également les consignes suivantes:
* - Toutes les fonctions doivent être pures
* - Tout doit être immutable (val au lieu de var)
* - Utiliser la recursion terminale si possible
* - Utiliser le pattern matching si possible
*/
object Serie3 {
/*
* Donne la longueur d'une liste. Votre implémentation ne peut
* utiliser aucune fonction de List excepté isEmpty()
*/
def len[A]( as: List[A] ): Int = {
@scala.annotation.tailrec
def size(rem: List[A], s: Int): Int = {
rem match {
case Nil => s
case _ :: bs => size(bs, s+1)
}
}
size(as, 0)
}
/*
* Inverse l'ordre des elements d'une liste. Votre implémentation ne peut
* utiliser aucune fonction de List excepté: -
* - isEmpty
* - ::
* - head
* - tail
*/
def rev[A]( as: List[A] ): List[A] = {
@scala.annotation.tailrec
def takeLast(rem: List[A], bs: List[A]): List[A] = {
rem match {
case Nil => bs
case a :: tail => takeLast(tail, a::bs)
}
}
takeLast(as, Nil)
}
/*
* Somme les elements d'une liste. Votre implémentation ne peut
* utiliser aucune fonction de List excepté: -
* - isEmpty
* - head
* - tail
*/
def sum( xs: List[Int] ): Int = {
@scala.annotation.tailrec
def acc(rem: List[Int], s: Int): Int ={
rem match{
case Nil => s
case a :: bs => acc(bs, s+a)
}
}
acc(xs, 0)
}
/*
* Retourne vrai si et seulement si la liste xs ne
* comprend que des valeures vraies. Votre implémentation
* ne peutcutiliser aucune fonction de List excepté:
* - isEmpty
* - head
* - tail
*/
@scala.annotation.tailrec
final def and(xs: List[Boolean] ): Boolean = {
xs match {
case Nil => true
case true :: bs => and(bs)
case _ => false
}
}
/*
* Applatit une liste. Votre implémentation
* ne peut utiliser aucune fonction de List excepté:
* - isEmpty
* - head
* - tail
* - ++
*
*/
def flat[A]( las: List[List[A]] ): List[A] = {
@scala.annotation.tailrec
def through(as: List[List[A]], dst: List[A]): List[A] = {
as match {
case Nil => dst
case Nil::bs => through(bs, dst)
case (head::tail):: bs => through(tail::bs, head::dst)
}
}
rev(through(las, Nil))
}
/*
* Retourne vrai si la Liste a une nombre pair d'éléments. Votre
* implémentation ne peut utiliser aucune fonction de List ! Vous
* devez utiliser le pattern matching.
*/
final def even[A]( as: List[A] ): Boolean = as match{
case Nil => true
case _ :: Nil => false
case _ :: _ :: rest => even(rest)
}
}
object Serie3Main extends App {
import Serie3._
val is = List( 1, 2, 3, 4, 5 )
val bs1 = List( true, true, false, true )
val bs2 = List( true, true, true )
val las1 = List.empty[List[Int]]
val las2 = List( List(1,2), List(3), List(4,5) )
println("1st")
require( rev(is) == List( 5, 4, 3, 2, 1 ) )
println("2nd ")
require( len(is) == 5 )
require( len( las1 ) == 0 )
require( len( bs1 ) == 4 )
println("3rd")
require( flat(las1) == Nil )
require( flat(las2) == is )
println("4th")
require( sum(is) == 15 )
require( sum(flat(las2)) == sum(is) )
println("5th")
require( rev( bs1 ) == List( true, false, true, true ) )
require( rev( bs2 ) == bs2 )
println("6th")
require( !even( is ) )
require( even( bs1 ) )
require( even( las1 ) )
}
/*
* Implémenter les fonctions suivantes en suivant les commentaires.
*/
object Predicates {
type P[A] = A=>Boolean
/*
* La méthode 'not' retourne un nouveau prédicat dont le résultat
* est toujours la négation du résultat de l'argument.
*/
def not[A]( p: A =>Boolean ): A =>Boolean = { a:A =>
!p(a)
}
/*
* La méthode 'and' retourne un nouveau prédicat dont le résultat
* est toujours la conjonction des résultats des deux arguments.
*/
def and[A](p1: A =>Boolean, p2: A =>Boolean ): A =>Boolean = { a:A =>
p1(a)&&p2(a)
}
/*
* La fonction 'or' retourne un nouveau prédicat dont le résultat
* est toujours la disjonction des résultats des deux arguments.
*/
def or[A](p1: A =>Boolean, p2: A =>Boolean ): A =>Boolean = { a:A =>
p1(a)||p2(a)
}
/*
* La fonction 'exists' retourne un nouveau prédicat dont le
* résultat est vrai si au moins un des prédicat de l'argument est
* vrai.
*/
def exists[A]( ps: List[A =>Boolean] ): A =>Boolean = { a:A =>
@scala.annotation.tailrec
def existRec(rest: List[P[A]], a: A): Boolean = rest match {
case Nil => false
case h :: _ if h(a) => true
case _ :: t => existRec(t, a)
}
existRec(ps, a)
}
/*
* La fonction 'forall' retourne un nouveau prédicat dont le
* résultat est vrai si et seulement si tous les prédicats de
* l'argument sont vrais.
*/
def forall[A]( ps: List[A =>Boolean] ): A =>Boolean = { a:A =>
@scala.annotation.tailrec
def forAllRec(rest: List[P[A]], a:A): Boolean = rest match{
case Nil => true
case h :: _ if !h(a) => false
case _ :: t => forAllRec(t, a)
}
forAllRec(ps, a)
}
}
import org.scalatest.funsuite.AnyFunSuite
import Predicates._
class PredicatesSuite4 extends AnyFunSuite {
val big = (_:Int) >= 100
val even = (_:Int) % 2 == 0
val small = not[Int]( big )
val bae = and[Int]( big, even )
val boe = or[Int]( big, even )
test("Predicate Evaluation") {
assert( big(200) )
assert( ! big(19) )
assert( even(200) )
assert( ! even(201) )
}
test("Predicate negation") {
assert( small(19) )
assert( !small( 200 ) )
}
test("Predicate AND") {
assert( bae( 200 ) )
assert( !bae( 201 ) )
}
test("Predicate OR") {
assert( boe( 201 ) )
assert( !boe( 19 ) )
}
val mul3 = (_:Int ) % 3 == 0
val ps = List( big, even, mul3 )
test("Predicates FORALL") {
assert( forall[Int]( ps )( 402 ) )
assert( ! forall[Int]( ps )( 200 ) )
}
test("Predicates EXISTS") {
assert( exists[Int]( ps )( 18 ) )
assert( ! exists[Int]( ps )( 1 ) )
}
}
File deleted
*~
target/
# Default ignored files
/workspace.xml
\ No newline at end of file
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="false" project-jdk-name="13 (2)" project-jdk-type="JavaSDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/modules/base_tp_6009.iml" filepath="$PROJECT_DIR$/.idea/modules/base_tp_6009.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/base_tp_6009-build.iml" filepath="$PROJECT_DIR$/.idea/modules/base_tp_6009-build.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="base_tp_6009-build" external.linked.project.path="$MODULE_DIR$/../../project" external.root.project.path="$MODULE_DIR$/../.." external.system.id="SBT" sbt.imports="" sbt.resolvers="$USER_HOME$/.ivy2/cache|ivy|Local cache" type="SBT_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/../../project/target/idea-classes" />
<output-test url="file://$MODULE_DIR$/../../project/target/idea-test-classes" />
<exclude-output />
<content url="file://$MODULE_DIR$/../../project">
<sourceFolder url="file://$MODULE_DIR$/../../project" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/../../project/project/target" />
<excludeFolder url="file://$MODULE_DIR$/../../project/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="sbt: sbt-and-plugins">
<CLASSES />
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
<component name="SbtModule">
<option name="buildForURI" value="file:$MODULE_DIR$/../../" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="base_tp_6009 [file:/media/quentin/Storage/Work/HEPIA/3rd_Year/BA-6/ParaProgAv/scala2020/base_tp/]" external.linked.project.path="$MODULE_DIR$/../.." external.root.project.path="$MODULE_DIR$/../.." external.system.id="SBT" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_13">
<output url="file://$MODULE_DIR$/../../target/dummy" />
<exclude-output />
<content url="file://$MODULE_DIR$/../..">
<excludeFolder url="file://$MODULE_DIR$/../../target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="scala-sdk-2.12.6" level="application" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ScalaSbtSettings">
<option name="customVMPath" />
<option name="linkedExternalProjectsSettings">
<SbtProjectSettings>
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/project" />
</set>
</option>
<option name="sbtVersion" value="1.2.1" />
<option name="useAutoImport" value="true" />
<option name="useQualifiedModuleNames" value="true" />
</SbtProjectSettings>
</option>
</component>
</project>
\ No newline at end of file