This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------------------------------------------- | |
/** | |
* Parses the given pattern and appends the rules to the given | |
* DateTimeFormatterBuilder. | |
* | |
* @param pattern pattern specification | |
* @throws IllegalArgumentException if the pattern is invalid | |
* @see #forPattern | |
*/ | |
private static void parsePatternTo(DateTimeFormatterBuilder builder, String pattern) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class RestServlet extends HttpServlet { | |
public void addConfiguration(String key, String value) { | |
response.put(key, value); | |
} | |
private Map<String, String> response = new HashMap<String, String>(); | |
public RestServlet() { | |
response.put("/autos", Arrays.asList("BMW", "Mercedes").toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def position[A](list: List[A], element: A) = { | |
val index: Int = list indexOf (element) | |
if (index == -1) Nil | |
else Just(index) | |
} | |
sealed abstract class Maybe[+A] { | |
def isEmpty: Boolean | |
def get: A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mapPatternMatching[A, B](xs: List[A], f: A => B): List[B] = { | |
xs match { | |
case List() => scala.Nil | |
case head :: tail => f(head) :: mapPatternMatching(tail, f) | |
} | |
} | |
//map(List(1, 2, 3), (x: Int) => x + 1) | |
def mapForComprehension[A, B](xs: List[A], f: A => B) = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util | |
import java.util.{Collection => JCollection, ArrayList} | |
class JavaToTraversable[E] (javaCollection: JCollection[E]) extends Traversable[E] { | |
override def foreach[U](f: (E) => U): Unit = { | |
val iterator = javaCollection.iterator() | |
while(iterator.hasNext) { | |
f(iterator.next()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val languages: Seq[String] = Seq("Scala", "Haskell", "OCaml", "ML") | |
languages(1) | |
val default: PartialFunction[Int, String] = { | |
case _=> "Is it a FP language???" | |
} | |
val languageWithDefault = languages orElse default | |
languageWithDefault(15) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def throwableToLeft[T](block: => T): Either[Throwable, T] = { | |
try { | |
Right(block) | |
} | |
catch { | |
case ex: Throwable => Left(ex) | |
} | |
} | |
val r = throwableToLeft { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val restrictedList = List(-2, -1, 0, 1, 2) map(2 / _) | |
restrictedList(0) | |
val nonrestrictedList = List(-2, -1, 0, 1, 2).view map(2 / _) | |
nonrestrictedList(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL | |
import scala.io.Source | |
import scala.xml.XML | |
def tweets(handle: String) = { | |
println("Fetching tweets for specified handle: " + handle) | |
val source = Source.fromURL(new URL("https://twitter.com/search?q=" + handle)) | |
val iterator: Iterator[String] = source.getLines() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val restrictedAccess: Map[String, Elem] = Map("scala" -> tweets("scala"), "java" -> tweets("java")) | |
val usingPartialFuncs: Map[String, (String) => Elem] = Map("scala" -> tweets _, "java" -> tweets _) | |
usingPartialFuncs.view.map{ t=> t._2(t._1) }.head |
OlderNewer