Skip to content

Instantly share code, notes, and snippets.

-- Function which takes the name of a month ("May") and the number of days in that month (31),
-- and returns a list of all the dates in that month (["May 1", ..., "May 31"]).
-- using map
datesInMonth :: String -> Int -> [String]
datesInMonth month days = map (\x -> month ++ " " ++ show x) [1..days]
-- using list comprehension
datesInMonth2 :: String -> Int -> [String]
datesInMonth2 month days = [ month ++ " " ++ show i | i <- [1..days]]
@rrodseth
rrodseth / UnfoldPages.scala
Last active June 14, 2021 18:36
Create akka-stream Source from a pagination, using Source.unfoldAsync
// Inspired by a tweet from @trautonen 1/13/2016
// Use Source.unfoldAsync to turn paginated database results into an akka-streams Source
// unfold is the inverse of fold
case class Page[T](pageNumber:Long, totalPages:Long, contents:List[T])
case class Thing(id: Long, name: String = "foo")
val totalPages = 5 //
val pageSize = 3
@rrodseth
rrodseth / Composing Futures
Last active December 13, 2015 19:39
Composing Futures with a for-comprehension
// Credit: http://blog.knoldus.com/2013/01/27/akka-futures-using-for-comprehensions/
import akka.actor.ActorSystem
import scala.concurrent.{future, Future, Await }
import scala.concurrent.ExecutionContext.Implicits.global
object FutureComposingWithFor extends App {
def longRunningFunction(number: Int) = {