Skip to content

Instantly share code, notes, and snippets.

@pierangeloc
pierangeloc / mongo-db-queries.js
Last active December 30, 2015 21:39
mongoDB queries compendium. This is an overview of the capabilities of Mongo that I have been using during the M101J course
//find from people collection
// _id can be not only a scalar value but also a document:
db.people.insert({_id: {"bsn": "2322323", "phone_nr" : "0612313123123"}})
/** QUERIES **/
// find documents where profession exists
db.people.find({profession: {$exists: true}})
// find documents where name attribute has type String (for the correspondence between String and type
//number, refer to http://bsonspec.org/#/specification)

##Shapeless

Type class derivation: applies to ADT(Alg Data Types). ADT is a sealed _empty_trait extended by case classes. Many advantages

Type class: trait indexed on a type T and provides some methods and defs on the type T. in the comp obj we define a def of the typed class for that type, and we can derive it through the implicitly

trait Eq[T] {
	def eqv(x: T, y: T): Boolean
}
@pierangeloc
pierangeloc / scala-intro-devjam.scala
Created March 22, 2016 22:08
REPL after the Gentle Scala Introduction @ Sytac DevJam
/**
* For the first part I had to rely on the repl history as the console buffer was gone after the stack overflow of factorial
*/
val i = 10
:type i
val i: Int = 10
val s = "Ciao"
i = 4
var j = 20
j = 10

Ammonite for Scala Scripting (@li_haoyi)

can do amm foo.sc, but also scala foo.sc. So why use it? Because I can import dependency on the fly. With scala I have to use -cp ... => Blah! And I can't import other scripts

####amm scripting

import $file.foo.bar` //import ./foo/bar.sc
foo.bar.methodOfBar(argument)

Dependent Typing (@Nick_enGB, @nstack)

(Algebraic Infrastructure company) ==> Major takeaway: Why DT matters

  • Express types that couldn't express otherwise Type system is to prove something about our program, through syntactical rules. So we prove things @ compile time. Typically we think of type in terms of how they look like on the memory structure. But type is more about guaranteeing behavior. E.g. a NEL has same shape as List, but gives one extra guarantee!

Aim: prove properties through type system (Curry-Howard): types are propositions (statements), and values are proofs of those proposition. Functions? Are implications: If input is A, output is of type B => it's a way where inputs are preconditions, outputs are postconditions.

Advanced Functional Programming with Scala - Notes

Copyright © 2016-2017 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
@pierangeloc
pierangeloc / ScalaX-2017-day-1.md
Last active December 15, 2017 08:37
ScalaX-2017-day-1

ScalaX 2017

Bartosz Milewski: Cat theory

Cat Theory: science of composition. Most of maths can be based on it, and programming as well.

Compose and abstract. We need to abstract because we can't hold more than 2-3 things and details at time in our head (short memory). We can't check every time the impl details.

FP

f: A => B. Can either base on Set theory, or forget about details and focus on substance, and I have object/arrows and properties.

@pierangeloc
pierangeloc / Kafka-cmd.md
Last active March 11, 2019 16:42
Useful Kafka console commands

Kafka 101

We use the reference topic named test, and the consumer group piero-group. Zookeeper runs on localhost:9092, Kafka bootstrap server is localhost:2181

  • Create a topic named test with 4 partitions, without replication

    ~/tools/kafka_2.11-1.1.0/bin/kafka-topics.sh --create --zookeeper localhost:2181 --partitions 4 --replication-factor 1 --topic test4
    
  • Produce String messages from console and post them on topic

@pierangeloc
pierangeloc / java-resources-access.md
Last active January 6, 2019 23:33
On resources access from packaged jvm apps
def loadResources(onBehalfOf: Class[_])(path: String): List[Path] = {
    val url: URL = onBehalfOf.getClassLoader.getResource(path)
    if (url.toURI.getScheme.contains("jar")) {
      val jar: URL = onBehalfOf.getProtectionDomain.getCodeSource.getLocation
      val jarFile: Path = Paths.get(jar.toString.substring("file:".length))
      val fs: FileSystem = FileSystems.newFileSystem(jarFile, null)
      val directoryStream: DirectoryStream[Path] = Files.newDirectoryStream(fs.getPath(path))
      directoryStream.asScala.toList
 } else {
@pierangeloc
pierangeloc / FutureTraverse.scala
Last active June 12, 2019 21:32
Don't use scala Future
trait TimedExecution {
implicit val materializer: Materializer
def delayed[A, B](f: A => B)(delay: FiniteDuration): A => Future[B] = a => Source.tick(delay, delay, a).take(1).map(f).runWith(Sink.head)
def delayedValue[A](a: => A)(delay: FiniteDuration): Future[A] = delayed[Unit, A](_ => a)(delay)(())
}