Skip to content

Instantly share code, notes, and snippets.

View NicolaeNMV's full-sized avatar

Nicu Namolovan NicolaeNMV

View GitHub Profile
@NicolaeNMV
NicolaeNMV / base64_binary_to_ascii_enc_dec.scala
Last active September 15, 2018 16:53
Encode/Decode Ascii->Binary->Base64 in Scala
// Multistep encoding of ascii to binary->base64
// As the binary will be presented as blocks of 0,1 separated by space.
// The base64 result will contain patterns such as MTA, MCA, IDE.
val message = "Hello World !"
def encode(message: String): String =
java.util.Base64.getEncoder().encodeToString( message.map(_.toBinaryString).mkString(" ").map(_.toByte).toArray )
def decode(base64: String): String =
@NicolaeNMV
NicolaeNMV / StreamBroadcastSampleAmm.scala
Created February 6, 2017 07:50
Playing with reactive streams and ammonite
#!/usr/bin/env amm
import $ivy.`com.typesafe.akka::akka-stream:2.4.16`
import java.nio.file.Paths
import akka.NotUsed
import akka.stream._
import akka.stream.scaladsl._
import akka.actor.ActorSystem
import akka.util.ByteString
@NicolaeNMV
NicolaeNMV / akka-intellij-worksheet-work-arround.md
Last active February 17, 2021 15:35
How to run akka actors in Intelij Worksheet

Missing resource

Scala code run from Intellij Worksheet doesn't seem to be able to find the akka actor resources.

The error is:

com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'akka'

There is an opened ticket on Intellij bugtracker https://youtrack.jetbrains.com/issue/SCL-9229 with a work arround

@NicolaeNMV
NicolaeNMV / start_notebook_with_spark.sh
Created January 7, 2017 08:51
Start jupyter notebook with spark
#!/bin/sh
export PYSPARK_DRIVER_PYTHON="/usr/local/bin/jupyter"
export PYSPARK_DRIVER_PYTHON_OPTS="notebook --NotebookApp.open_browser=False --NotebookApp.ip='*' --NotebookApp.port=8880"
export PYSPARK_PYTHON="/usr/bin/python"
pyspark
@NicolaeNMV
NicolaeNMV / gist:6909e217df58a85791a5
Last active February 14, 2016 11:17
Slick mysql playframework 2.4 configure for emoji
The default utf8 character set doesn't support emoji characters, you'll see `[SQLException: Incorrect string value: '\xF0\x9F\x98\x8A P...' for ` exception when trying to insert data.
You'll need utf8mb4 character set to be able to sotre emoji characters.
1. You'll need to create all the tables with CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci option
2. After initializing the conneciton, you will need to send SET NAMES utf8mb4,
you can do that with the hikari connectionInitSql option, a SQL statement that will be executed after the connetion was initialized.
slick.dbs.default.db.connectionInitSql="set names utf8mb4"
@NicolaeNMV
NicolaeNMV / gist:505f6cb991e37d2a3b37
Last active August 29, 2015 14:22
Activate DI in playframework 2.4

In playframework 2.4, if you get the error

object Application is not a member of package controllers
Note: class Application exists, but it has no companion object.

It's because the class is not instantiated. You need to add

routesGenerator := InjectedRoutesGenerator

@NicolaeNMV
NicolaeNMV / gist:d3d9d9958b7769e8d48a
Created May 29, 2015 12:26
reactive-stream FlowMaterializer

If you were playing with reactive-stream, and you got this error

scala> val sum = source.runWith(sink)
<console>:14: error: could not find implicit value for parameter materializer: akka.stream.FlowMaterializer

You need an implicit FlowMaterializer

def runWith[Mat2](sink: Graph[SinkShape[Out], Mat2])(implicit materializer: FlowMaterializer): Mat2

@NicolaeNMV
NicolaeNMV / simple.js
Last active August 29, 2015 14:15
Flow type VS Typescript at detecting non existing JSON elements
/* @flow */
var a = {
"a":"a",
"b":"b"
}
var res = a.c;
// $ flow simple.js
// $ No errors!
@NicolaeNMV
NicolaeNMV / fix_bsonobjectid_implicit.scala
Created January 10, 2015 14:16
Solve ReactiveMongo playframework scala "No implicit format for reactivemongo.bson.BSONObjectID available."
import play.api.libs.json.{Json, Format}
// If you imported
import play.modules.reactivemongo.json.BSONFormats.BSONObjectIDFormat
// And still getting No implicit format for reactivemongo.bson.BSONObjectID available. error
// Check that your JSON formatter is typed. Change
implicit val mongoFormat = Json.format[Category]
// to
implicit val mongoFormat: Format[Category] = Json.format[Category]
@NicolaeNMV
NicolaeNMV / play2dynamicRouteCheck.scala
Last active August 29, 2015 14:06
Check dynamically if a route path exists in play2 framework
/*
routes:
GET /test controllers.Application.test(route: String)
GET /guessThat controllers.Application.guessIt
// To check:
/test?route=/guessIt