Skip to content

Instantly share code, notes, and snippets.

@riccardomerolla
riccardomerolla / Fibonacci.scala
Last active September 8, 2022 06:59
Fibonacci
def fibonacci(num: Int): BigInt =
@scala.annotation.tailrec
def fibFcn(n: Int, acc1: BigInt, acc2: BigInt): BigInt = n match
case 0 => acc1
case 1 => acc2
case _ => fibFcn(n - 1, acc2, acc1 + acc2)
fibFcn(num, 0, 1)
fibonacci(90)
@riccardomerolla
riccardomerolla / Tree.scala
Created September 8, 2022 06:57 — forked from dholbrook/Tree.scala
Scala binary tree
/**
* D Holbrook
*
* Code Club: PO1
*
* (*) Define a binary tree data structure and related fundamental operations.
*
* Use whichever language features are the best fit (this will depend on the language you have selected). The following operations should be supported:
*
* Constructors
'use strict';
angular.module('app').config(['$routeProvider', function ($routeProvider) {
var routes =[{
url: '/dashboard',
config: {
template: '<dashboard></dashboard>'
}
},
@riccardomerolla
riccardomerolla / RESTClient.java
Last active August 29, 2019 06:36
Pure Java REST Client
package util;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
val jsonString = """{"count":100205,"_shards":{"total":5,"successful":5,"failed":0}}"""
val tweets = scala.util.parsing.json.JSON.parseFull(jsonString)
def getValue(parsedJson: Option[Any], key: String): Double = {
parsedJson match {
case Some(m: Map[String, Any]) => m(key) match {
case d: Double => d
}
}
@riccardomerolla
riccardomerolla / oracle_grant_targetuser
Created January 8, 2014 17:06
Store procedure to add sourceuser grant to targetuser
BEGIN
FOR Rec IN (SELECT object_name, object_type FROM all_objects WHERE owner='SOURCEUSER' AND object_type IN ('TABLE','VIEW','PROCEDURE','FUNCTION','PACKAGE')) LOOP
IF Rec.object_type IN ('TABLE','VIEW') THEN
EXECUTE IMMEDIATE 'GRANT SELECT, UPDATE, INSERT, DELETE ON SOURCEUSER.'||Rec.object_name||' TO TARGETUSER';
ELSIF Rec.object_type IN ('PROCEDURE','FUNCTION','PACKAGE') THEN
EXECUTE IMMEDIATE 'GRANT EXECUTE ON SOURCEUSER.'||Rec.object_name||' TO TARGETUSER';
END IF;
END LOOP;
END;
@riccardomerolla
riccardomerolla / scala-file-groupby-keyword.scala
Last active December 26, 2015 01:29
Scala file splitter/group by keyword
//val lines = scala.io.Source.fromFile("test.txt", "utf-8").getLines.toList
val lines = List("HEAD 000001","HEAD 000002","HEAD 000002","HEAD 000002","HEAD 000003","HEAD 000003")
val keywords = lines.map(x => x.substring(9, 15)).distinct
def groupByKeyword(lines: List[String], keywords: List[String]): List[List[String]] = keywords match {
case keyword :: Nil => lines.groupBy(x => x.substring(9, 15).contains(keyword)).get(true).toList
case keyword :: _keywords => {
val mapped = lines.groupBy(x => x.contains(keyword))
mapped.get(true).toList.head :: groupByKeyword(mapped.get(false).toList.head, _keywords)