Skip to content

Instantly share code, notes, and snippets.

View rberenguel's full-sized avatar

Ruben Berenguel rberenguel

View GitHub Profile
@rberenguel
rberenguel / wat.scala
Created March 8, 2019 09:58
Weird companion object issue in Scala 2.11 vs 2.12
case class Country(name: String) {
private val ThreeLetterValidCountries = List("FOO", "BAR")
def valid: Option[Country] =
if (ThreeLetterValidCountries.contains(name.toUpperCase)) Some(this) else None
}
object Country {
def apply(name: String): Country = new Country(name.toUpperCase)
}
@rberenguel
rberenguel / primes.py
Created September 24, 2018 22:04 — forked from vegard/primes.py
Prime factorisation diagram
# -*- coding: utf-8 -*-
#
# Author: Vegard Nossum <vegard.nossum@gmail.com>
import math
import os
import sys
import cairo
case class Foo(id: String, value: Int)
case class Bar(theId: String, value: Int)
val ds = List(Foo("Alice", 42), Foo("Bob", 43)).toDS
import org.apache.spark.sql.{DataFrame, Dataset}
val renamedDF: DataFrame = ds.select($"id".as("theId"), $"value")
val renamedDS: Dataset[Bar] = renamedDF.toDF("theId", "value").as[Bar]

Keybase proof

I hereby claim:

  • I am rberenguel on github.
  • I am rberenguel (https://keybase.io/rberenguel) on keybase.
  • I have a public key ASDoKc_uTbL4lQBaJ4bNh9G-5OkiK2gSxIFF6h0xvuMy8wo

To claim this, I am signing this object:

@rberenguel
rberenguel / schemaAsScala.scala
Last active March 8, 2018 14:57
Converts a DataFrame schema output (StructType) into something you can paste as a schema, as valid Scala code. Useful when working with Apache Zeppelin or any other REPL.
import org.apache.spark.sql.types._ // You'll need this to evaluate its output
object StructFmt {
def asScala(field: StructField): String = field.dataType match {
case struct: StructType => s"""StructField("${field.name}",""" + asScala(struct) + s", ${field.nullable})"
case _ => s"""StructField("${field.name}", ${field.dataType}, ${field.nullable})"""
}
def asScala(struct: StructType): String = "StructType(Seq(" + (for(field <- struct) yield asScala(field)).mkString(",") + "))"
}
@rberenguel
rberenguel / git_complete.sh
Last active September 3, 2017 09:02
Fzf (see https://github.com/junegunn/fzf) completion for git checkout (which I have aliased to co). See (a previous version without origin removal) in action here: https://asciinema.org/a/DCZH8q4mZHr79uVGZcM5RCtRb
_fzf_complete_git() {
ARGS="$@"
local branches
branches=$(git branch -vv --all | sed "s/remotes\/origin\///g" | sed "s/\*/ /g")
if [[ $ARGS == 'git co'* ]]; then
_fzf_complete "--reverse --multi" "$@" < <(
echo $branches
)
else
eval "zle ${fzf_default_completion:-expand-or-complete}"
@rberenguel
rberenguel / fun_with_ast.py
Last active January 8, 2017 17:57
Having some quick fun with the ast (and being really impressed by how grumpy (https://github.com/google/grumpy/tree/master/compiler) manages to do it so neatly for python-go, this is a quick hack to play with ast). Enters a simple (almost hardcoded for this example) class, outputs a scala-valid class that you can try on an interpreter.
from __future__ import print_function
import ast
"""
Save the following in test.py, then execute fun_with_ast.py.
You can paste the output in a Scala interpreter
class something(object):
def __init__(self, value1, value2):
@rberenguel
rberenguel / SafariTabSwitcher.lua
Last active April 1, 2022 15:45
An interactive tab chooser for Safari written with AppleScript (yikes) for HammerSpoon (Lua scripting framework for Mac OS automation)
----
-- A tab chooser for Safari written with AppleScript (yikes) and HammerSpoon
----
----
-- Known issues:
-- If a tab has double quotes, unsure about what will happen, the tab
-- list creation is brittle. Single quote is fixed though
@rberenguel
rberenguel / Instapaper_dumper
Last active August 29, 2015 14:22
Dump (if correctly cleaned up) instapaper links from a folder-category to a folder
csvfix find -e 'Videos' -f 4 ~/Downloads/instapaper-export.csv | csvfix edit -e "s/[ ,.|:'\?]/_/g" -f 2 | csvfix order -f 2,1,3,4 | csvfix printf -fmt "wget --directory-prefix='./%s/' -T 60 -q -p -k %s" > x;cat x;chmod +x x;./x;rm ./x
@rberenguel
rberenguel / mu4e_niceties.el
Last active August 29, 2015 14:08
The nicest would be :ft, which shows from/to depending on the source, hence making "sent" a more useful folder (since I'm pretty sure the "From" field in "Sent" is "me"
(setq mu4e-bookmarks
'( ("flag:unread AND NOT flag:trashed AND date:today..now AND maildir:/INBOX" "Today's inbox" ?i)
("date:today..now AND NOT maildir:/INBOX.Sent" "Today's messages" ?t)
("date:30d..now AND maildir:/INBOX.Sent" "Sent last 30 days" ?s)
("date:7d..now" "Last 7 days" ?w)
("mime:image/*" "Messages with images" ?p)
("size:1M..500M" "Big messages" ?b))
)
(add-to-list 'mu4e-header-info-custom