Skip to content

Instantly share code, notes, and snippets.

@tabdulradi
tabdulradi / namespace.py
Last active December 10, 2015 10:48
Provides an adapter to allow using a class object in name-spacing your config constants conveniently . It allows you to use the dot notation to refer to your conf/constant, yet you can use features like key-word args unpacking too. It supports name-space inheritance from regular classes.
from collections import Mapping
from itertools import ifilter
class Namespace(Mapping):
'''
Provides an adapter to use a class as dict, and object at the same time.
It allows you to use the dot notation to refer to your conf/constant,
yet you can use features like key-word args unpacking too.
@tabdulradi
tabdulradi / calc.py
Last active December 21, 2015 19:19
Python Using a dict instead of Switch statements
def switch(cases, case, default_case, *args, **kwargs):
return cases.get(case, default_case)(*args, **kwargs)
def power(x):
return x ** 2
def add(x, y):
return x + y
def illegal_operator(*args, **kwargs):
import akka.actor._
class FibonacciGenerator extends Actor {
import FibonacciGenerator._
def receive = fib()
def fib(a: Long = 0, b: Long = 1): Receive = {
case Next =>
val c: Long = a + b
@tabdulradi
tabdulradi / rational.js
Last active August 29, 2015 14:14
SICP using JS > rational
function pair(a, b) {
return function(i) {
if (i === 0) return a
else return b
}
}
function head(p) {
return p(0)
}
function tail(p) {
public final class ImmutableDummy {
public final Integer x;
public final Option<Integer> y;
@JsonCreator
public ImmutableDummy(@JsonProperty("x") Integer x,
@JsonProperty("y") Integer y) {
this.x = x;
this.y = Option.apply(y);
@tabdulradi
tabdulradi / ComposingRoutes.scala
Last active May 23, 2016 12:35
Cake Blog: All you need to know about Play SIRD
val service1 = Router.from {
case GET(p"hello/$to") => ???
}
val service2 = Router.from {
case GET(p"echo/$msg") => ???
}
@tabdulradi
tabdulradi / CompositeRouter.scala
Last active March 5, 2018 06:08
Utility Play routes that makes it prettier to combine routes together
class CompositeRouter(routers: Seq[Router]) extends SimpleRouter {
override def documentation: Seq[(String, String, String)] =
routers.flatMap(_.documentation)
override def routes: Router.Routes =
routers.map(_.routes).fold(Router.empty.routes)(_ orElse _)
}
object CompositeRouter {
def fromPrefixedRoutes(routers: (String, Router.Routes)*): CompositeRouter =
@tabdulradi
tabdulradi / troy-abstract.md
Last active November 15, 2016 11:09
Troy Abstract

Introducing Troy: The schema-safe Cassandra toolkit.

Troy is an open source macro-based Cassandra driver, similar to Slick and Quill, provides type-safe & compile-time checking for database queries. Nevertheless, it doesn't impose a DSL to express the queries in Scala. Instead, it allows developers to write plain Cassandra-query-language (CQL) queries within Scala code, complete with schema validation. In addition, it provides cross-validation against the previous versions, ensuring safe and smooth schema migrations.

It is worth noting that Troy doesn't connect to Cassandra during compilation. Instead, the schema is provided as CQL scripts, checked-in within same code base, consisting of plain CREATE TABLE CQL statements. That get's loaded at compile-time into a light-weight Schema engine capable of analysing queries and providing information about column types. Those CQL scripts can be written as increments, by adding new scripts containing `ALTER TA

@tabdulradi
tabdulradi / range-long-bug.scala
Last active November 19, 2016 21:24
Scala Range.Long min/max bug
scala> Range.Long.inclusive(111, 999, 1).max
res27: Long = 999
scala> Range.Long.inclusive(111, 999, -1).max
res28: Long = 111
scala> Range.Long.inclusive(111, 999, 1).min
res29: Long = 111
scala> Range.Long.inclusive(111, 999, -1).min
@tabdulradi
tabdulradi / keybase.md
Created October 7, 2017 09:44
keybase.md

Keybase proof

I hereby claim:

  • I am tabdulradi on github.
  • I am tabdulradi (https://keybase.io/tabdulradi) on keybase.
  • I have a public key ASBkIdYjb4IRe7DXOtrp_1ZSev9WYKBiZ8R-iSWKWa8kAAo

To claim this, I am signing this object: