Skip to content

Instantly share code, notes, and snippets.

View cb372's full-sized avatar

Chris Birchall cb372

View GitHub Profile
alert('hi')

Keybase proof

I hereby claim:

  • I am cb372 on github.
  • I am cb372 (https://keybase.io/cb372) on keybase.
  • I have a public key ASCYMnGniI49GycDeF7wL-osskKrzmEbfoRW25L5Xe1H_wo

To claim this, I am signing this object:

@cb372
cb372 / thoughts.md
Created June 5, 2017 21:36
Random thoughts on writing my first Rust app

Preamble

  • Background: I've been writing mostly Scala for the last few years, mostly in FP style. I also have experience writing Java, Ruby, Python, bash and a few other languages.

  • Caveat: anything below which sounds like a grumble about the language is probably due to ignorance on my part. Please advise/correct me in the comments.

  • For a more eloquent writeup of a similar topic by somebody who can actually write Rust, see Lloyd's excellent blogpost

Amble

Welcome to the Ammonite Repl 0.8.2
(Scala 2.12.1 Java 1.8.0_121)
@ import $ivy.`org.typelevel::cats:0.9.0`, cats._, cats.implicits._
import $ivy.$ , cats._, cats.implicits._
@
@ trait Maths[F[_]] {
def int(i: Int): F[Int]
def add(l: F[Int], r: F[Int]): F[Int]
}
defined trait Maths
@cb372
cb372 / sequence.scala
Last active October 26, 2020 12:52
Using Cats Traverse to turn a List of Try into a Try of List
Welcome to the Ammonite Repl 0.8.0
(Scala 2.11.8 Java 1.8.0_91)
@ import scala.util.Try
import scala.util.Try
@ val listOfTries: List[Try[String]] = List(Try("a"), Try("b"), Try("c"))
listOfTries: List[Try[String]] = List(Success("a"), Success("b"), Success("c"))
// I have a List[Try[String]] but I actually want a Try[List[String]].
@cb372
cb372 / 0_PrettyPrinting.scala
Created October 7, 2016 11:27
Prettier AST trees for debugging macros
import scala.reflect.macros.blackbox
object PrettyPrinting {
/*
* Print a raw AST, nicely indented.
* Pretty fragile, e.g. will get confused by string literals containing commas or parentheses.
*/
def prettyTree(raw: String): String = {
var level = 0
@cb372
cb372 / gist:936caf91e69f1aed9513d2179418b4cd
Created August 17, 2016 13:47
Descriptions of Donald Trump as "reality TV star" in Guardian articles over time
$ curl 'https://content.guardianapis.com/search?tag=us-news/donaldtrump&q=%22reality%20tv%20star%22&page-size=200&from-date=2015-06-01&api-key=<api-key>' | \
jq -r '.response.results[].webPublicationDate' | \
cut -c1-7 | \
sort | \
uniq -c | \
gnuplot -e 'set terminal png; set output "test.png"; set xdata time; set timefmt "%Y-%m"; set xrange ["2015-06":"2016-08"]; set format x "%m/%Y"; plot "<cat" using 2:1 with lines title "Reality TV star"'
@cb372
cb372 / gist:c107a0d732254010f9f002080757e036
Created August 10, 2016 13:42
Use the ss command to display socket statistics
$ ss -s
Total: 185 (kernel 0)
TCP: 43 (estab 28, closed 7, orphaned 0, synrecv 0, timewait 6/0), ports 0
Transport Total IP IPv6
* 0 - -
RAW 0 0 0
UDP 14 8 6
TCP 36 3 33
INET 50 11 39
@cb372
cb372 / frequent.txt
Created June 9, 2016 13:48
1000 most frequent words in recent Guardian articles in "UK news" section
the: 458186
to: 222092
of: 201833
and: 184094
a: 175172
in: 158131
was: 83367
that: 82857
for: 73595
on: 71485
import scala.meta._
import java.nio.file._
object CommentWordCount extends App {
val scalaDir = "/Users/chris/code/scala"
val stdlibSrcDir = s"$scalaDir/src/library/scala"
val scalaFileContents: Array[String] = Files.walk(Paths.get(stdlibSrcDir)).toArray.collect {
case p: Path if p.toString.endsWith(".scala") => new String(Files.readAllBytes(p), "UTF-8")