Skip to content

Instantly share code, notes, and snippets.

View beckgael's full-sized avatar
🎯

Beck Gaël beckgael

🎯
View GitHub Profile
object RandomAccessList {
type RandomAccessList[A] = List[(CompleteBinaryTree[A], Int)]
def fromList[A](xs: List[A]): RandomAccessList[A] = {
def sublists[A](xs: List[A]): List[(List[A], Int)] = {
def inner(xs: List[A], size: Int, acc: List[(List[A], Int)]): List[(List[A], Int)] = size match {
case 0 => acc
case s => {
val s_ = Math.pow(2, Math.floor(Math.log(s + 1) / Math.log(2))).toInt - 1
inner(xs.take(s - s_), s - s_, (xs.drop(s - s_), s_)::acc)
@baseerhk
baseerhk / ral.scala
Last active November 8, 2019 09:05
Complete binary tree Scala implementation
sealed trait CompleteBinaryTree[A] {
def lookup(size: Int, index: Int): A = (this, index) match {
case (Leaf(x), 0) => x
case (Leaf(_), _) => throw new Exception("Index is not in list.")
case (Node(x, _, _), 0) => x
case (Node(x, l, _), i) if i <= size / 2 => l.lookup(size / 2, i - 1)
case (Node(x, _, r), i) if i > size / 2 => r.lookup(size / 2, i - 1 - size / 2)
}
def update(size: Int, index: Int, y: A): CompleteBinaryTree[A] = (this, index) match {
case (Leaf(_), 0) => Leaf(y)
@tyrcho
tyrcho / Main.scala
Last active January 26, 2021 11:01 — forked from anonymous/Main.scala
Scala.js Asynchronous REST call (Ajax) demo with JSON parsing - http://www.scala-js-fiddle.com/gist/484e6ca68976aadb2cac63b55069acd8
import util._
import dom.ext._
import scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow
object ScalaJSExample extends js.JSApp{
def main(): Unit = {
val url =
"http://jsonplaceholder.typicode.com/posts/1"
val f=Ajax.get(url)
f.onComplete{
@jkpl
jkpl / Main.scala
Last active February 5, 2024 08:29
Ways to pattern match generic types in Scala
object Main extends App {
AvoidLosingGenericType.run()
AvoidMatchingOnGenericTypeParams.run()
TypeableExample.run()
TypeTagExample.run()
}
class Funky[A, B](val foo: A, val bar: B) {
override def toString: String = s"Funky($foo, $bar)"
}
@eyecatchup
eyecatchup / git-commit-log-stats.md
Last active June 18, 2024 08:05
Some commands to get git commit log statistics for a repository on the command line.

git commit stats

Commands to get commit statistics for a Git repository from the command line -
using git log, git shortlog and friends.




@odersky
odersky / A simpler way to returning the "current" type in Scala.
Last active April 5, 2024 13:34
A simpler way to returning the "current" type in Scala.
/** This is in reference to @tploecat's blog http://tpolecat.github.io/2015/04/29/f-bounds.html
* where he compares F-bounded polymorphism and type classes for implementing "MyType".
*
* Curiously, the in my mind obvious solution is missing: Use abstract types.
*
* A lot of this material, including an argument against F-bounded for the use-case
* is discussed in:
*
* Kim B. Bruce, Martin Odersky, Philip Wadler:
* A Statically Safe Alternative to Virtual Types. ECOOP 1998: 523-549
@ElectricCoffee
ElectricCoffee / SimpleHashGenerator.scala
Last active March 9, 2022 10:39
A very simple checksum generator written in scala, the following checksum types have been tested: MD2 MD5 SHA SHA-256 SHA-512
package your.pkg.here
import java.security.MessageDigest
import java.nio.file.{Files, Paths}
object Generator {
implicit class Helper(val sc: StringContext) extends AnyVal {
def md5(): String = generate("MD5", sc.parts(0))
def sha(): String = generate("SHA", sc.parts(0))
def sha256(): String = generate("SHA-256", sc.parts(0))
@VladUreche
VladUreche / gist:8396624
Created January 13, 2014 08:39
Scaladoc tutorial for docs.scala-lang.org, in a pitiful state
# Scaladoc Developer Guide
## Introduction
Scaladoc is the tool that enables developers to automatically generate documentation for their Scala (and Java) projects. It is Scala's equivalent of the widely-used Javadoc tool. This means that Javadoc (and even doxygen) users will be familiar with Scaladoc from day 1: for them, it is most beneficial to check out the Scaladoc/Javadoc comparison tables and if necessary, skim through this document to understand specific features.
The rest of this tutorial is aimed at developers new to Scaladoc and other similar tools. It assumes a basic understanding of the Scala language, which is necessary to follow the examples given throughout the tutorial. For the user perspective on the Scaladoc-generated documentation, such as finding a class, understanding the page layout, navigating through diagrams, please refer to the Scaladoc User Guide.
The tutorial will start by a short motivation and then will explain the main concept in Scaladoc: the doc comment.
### Why document?