Skip to content

Instantly share code, notes, and snippets.

@4lex1v
4lex1v / init.el
Last active March 10, 2023 14:50
(require 'seq)
(require 'dired)
(require 'cl-lib)
(setq
gc-cons-threshold (* 100 1024 1024) ;; 100MB
read-process-output-max (* 1 1024 1024)) ;; 1MB
(defconst USER-EMACS-DIRECTORY (file-name-directory (or load-file-name (buffer-file-name))))
(defconst USER-INIT-FILE (concat USER-EMACS-DIRECTORY "init.el"))
@4lex1v
4lex1v / binding_directives.scala
Created February 16, 2016 14:27
Code for the article
/**
* For simplity i'm using Ammonite-Repl.
* https://lihaoyi.github.io/Ammonite/
*/
// Add Spray repo resolver
import ammonite.repl._, Resolvers._
resolvers() = resolvers() :+ Resolver.Http("spray repo", "http://repo.spray.io", IvyPattern, false)
// Load toolkit
load.ivy("io.spray" %% "spray-routing" % "1.3.3")
object Distribution {
case class DistributionConfig(
outputDirectory: File,
configResources: Seq[File], // from src/main/resources
distJvmOptions: String,
distMainClass: String,
libFilter: File ⇒ Boolean,
additionalLibs: Seq[File])
object DistributionKeys {
package io.alterstack.build
import sbt._
import Keys._
import com.typesafe.config.{Config, ConfigFactory}
import com.decodified.scalassh.{
Command => SshCommand,
CommandResult => SshCommandResult,
_
}
@4lex1v
4lex1v / spark-error.scala
Created November 16, 2015 10:30
Code for demystifying spark serization error article
/**
* Model for our case.
* Note that [[Value]] class is not serializable, while
* [[Wrapper]] is a case class, which is serializable by default.
*/
class Value
case class Wrapper(v: Value)
// defined class Value
// defined class Wrapper
@4lex1v
4lex1v / defaults.scala
Created November 10, 2015 11:25
Using default type parameter to drive type inference and a type class instance resultion
trait DefaultsTo[Type, Default]
object DefaultsTo {
implicit def defaultDefaultsTo[T]: DefaultsTo[T, T] = null
implicit def fallback[T, D]: DefaultsTo[T, D] = null
}
trait Document
trait Reader[A]
@4lex1v
4lex1v / C3BMCompute.scala
Created March 14, 2015 19:15
Compute bloom filter bits for Coursera C3 hm question
def compute(nums: Int*) = {
def h(idx: Int)(num: Int): Int = (((math.pow(num, 2) + math.pow(num, 3)) * idx) % 32).toInt
val fs: List[Int => Int] = List(h(1), h(2), h(3))
val result = nums.map { n =>
val bits = fs.map(_(n)).sorted
println(s"$n -> ${bits.mkString(",")}")
(n -> bits)
}
println(s"Result: ${result.map(_._2).flatten.sorted.distinct.mkString(",")}")
result
@4lex1v
4lex1v / gist:3d8e9d56108c2394a2c7
Created September 18, 2014 17:32
Variance, bottom types and type inference in Scala
/*****************************
* Invariant type with Nothing *
****************************/
sealed trait Interact[A]
case class Ask(value: String) extends Interact[String]
case class Tell(value: String) extends Interact[Nothing] // Notice Nothing here
val instr =
List(Ask("What's your first name?"),
@4lex1v
4lex1v / cake.scala
Created June 24, 2014 15:34
Small silly cake
trait Service {
def host: String
def port: Int
}
trait GoogleServiceModule extends Service {
val host = "google.com"
val port = 80
}
@4lex1v
4lex1v / streamTMarshaller.scala
Last active August 29, 2015 14:00
StreamT marshaller
implicit def streamTMarshaller[M[+_]](implicit mtm: MarshallerM[M], M: Monad[M], arf: ActorRefFactory) = {
new MarshallerM[({ type λ[α] = StreamT[M, α] })#λ] {
override def marshaller[T](implicit mt: Marshaller[T]): Marshaller[StreamT[M, T]] = {
new Marshaller[StreamT[M, T]] {
override def apply(value: StreamT[M, T], ctx: MarshallingContext): Unit = {
implicitly[MarshallerM[M]].marshaller[Stream[T]].apply(value.toStream, ctx)
}
}
}
}