Skip to content

Instantly share code, notes, and snippets.

View alexandru's full-sized avatar
😺
Having fun

Alexandru Nedelcu alexandru

😺
Having fun
View GitHub Profile
/** Lightweight encoding for the "Priority" pattern.
*
* This type will attempt to provide an implicit instance of `P`
* (the preferred type). If that type is not available it will
* fallback to `F` (the fallback type). If neither type is available
* then a `Priority[P, F]` instance will not be available.
*
* As described in the
* [[https://github.com/typelevel/algebra/ Typelevel Algebra]]
* project, but with a lighter encoding that does not require
import java.io._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.control.NonFatal
import scala.concurrent.duration._
import scalaz._
import scalaz.effect._
object Sample extends SafeApp {
def run(args: List[String]): IO[Unit] =
for {
/**
* Asynchronous Semaphore, made to work with Promises.
*
* ```
* const delay = (ms: number) =>
* new Promise<void>(_ => setTimeout(_, ms))
*
* const semaphore = new AsyncSemaphore(10)
* const promises: Promise<void>[] = []
@alexandru
alexandru / web-crawler.ts
Last active January 7, 2021 12:42
Web crawler that downloads HTML content (for analysis) from a list of websites, exporting content as JSON lines
#!/usr/bin/env node
import { IO, Try, Success, Failure, Either, Left, Right, Cancelable, Duration } from "funfix"
import { RequestResponse } from "request"
import * as fs from "fs"
import * as request from "request"
import * as Url from "url"
import * as cheerio from "cheerio"
import * as minimist from "minimist"
@alexandru
alexandru / A-EMACS-MAC-OS.md
Last active May 14, 2017 22:41
Getting emacsclient to work with https://emacsformacosx.com

Getting emacsclient to work on MacOS

  1. Download Emacs from https://emacsformacosx.com or use your favorite distribution, but then make sure to have an EMACSCLIENT path exported
  2. Create ~/bin/emacsclient
  3. Create ~/bin/edit
  4. Make sure ~/bin is on your PATH and has precedence over /usr/bin (otherwise the default MacOS emacsclient will be visible, which you should avoid)
  5. Edit your emacs.el
@alexandru
alexandru / covariance-and-higher-kinds.scala
Last active March 17, 2017 09:16
Scala's Covariance Rules are Maybe Incompatible with Higher-Kinds :-(
import scala.language.higherKinds
sealed abstract class Stuff[F[_], +A]
case class Suspend[F[_], A](a: () => F[A])
extends Stuff[F, A]
// This function fails compilation compilation with:
//
// found : Suspend[F,?A1] where type ?A1 <: A (this is a GADT skolem)
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import scala.util.Random
@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class IteratorBenchmark {
@Param(Array("8"))
var cycles = 0
import monix.eval.Task
import java.util.concurrent.TimeUnit
import scala.concurrent.duration._
/** Request limiter for APIs that have quotas per second, minute, hour, etc.
*
* {{{
* // Rate-limits to 100 requests per second
* val limiter = TaskLimiter(TimeUnit.SECONDS, limit = 100)
*
import scalaz._
import scalaz.concurrent.Task
def signal(i: Int): Task[Int] =
Task.async[Int] { cb => cb(\/-(i)) }
def loop(n: Int, acc: Int): Task[Int] =
signal(acc + n).flatMap { x =>
if (n <= 0) Task.now(acc)
else loop(n-1, x)
package shade.local
import shade.local.ImmutableCache.{Timestamp, Value}
import scala.annotation.tailrec
import scala.collection.immutable.SortedMap
import scala.concurrent.duration._
/** Describes an immutable cache data-structure.
*
* It behaves much like a standard `scala.collection.immutable.Map`, but