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
sealed trait State { def count: Int }
case class Odd(count: Int) extends State
case class Even(count: Int) extends State
case class Emit(count: Int) extends State
val scanned =
observable.scan(Empty : State) { (state, elem) =>
val newState = state match {
case Odd(count) =>
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 java.security.MessageDigest
import java.util
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import org.apache.commons.codec.binary.Base64
/**
* Sample:
* {{{
* scala> val key = "My very own, very private key here!"

How to add a Web Feed (RSS/Atom) to your Jekyll website

If you're generating your website with Jekyll or another static website generator, don't forget to also expose a web feed for us old timer using RSS readers. The format we'll be preferring here is Atom.

Some reasons to expose a web feed:

  1. people using RSS readers regularly are pretty loyal, RSS/Atom subscribers being like a mailing list for some of us
  2. tooling knows RSS/Atom — e.g. the easiest way to build a mailing list is to give Mailchimp your RSS feed

If you're looking for a recommendation on online RSS readers, I personally like Newsblur.

import java.io.{File, FileInputStream, FileOutputStream}
import monix.eval.Task
import monix.execution.Ack
import monix.execution.Ack.{Continue, Stop}
import monix.reactive.{Consumer, Observable, Observer}
import scala.util.control.NonFatal
def copyFile(input: File, destination: File, chunkSize: Int): Task[Unit] =
Task.defer {
val in = new FileInputStream(input)
@alexandru
alexandru / ExecutorServiceWrapper.scala
Last active March 17, 2021 23:52
Wrapping a scala.concurrent.ExecutionContext into java.concurrent.ExecutorService
import concurrent.{Future, Await, Promise, ExecutionContext}
import java.util.concurrent.{Future => JavaFuture, TimeUnit, Callable, CancellationException, ExecutorService}
import concurrent.duration._
import scala.util.Success
import scala.util.Failure
import scala.util.control.NonFatal
import scala.collection.JavaConverters._
import concurrent.duration.TimeUnit
import java.{util => jutil}
@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"

See context.

To configure this script to automatically execute on file changes, save the XML config in ~/Library/LaunchAgents/dot-files.sync.plist then do:

launchctl load -w ~/Library/LaunchAgents/dot-files.sync.plist