Skip to content

Instantly share code, notes, and snippets.

View sergiilagutin's full-sized avatar

Sergii Lagutin sergiilagutin

  • Alicante, Spain
View GitHub Profile
val str = "first string"
val field = classOf[String].getDeclaredField("value")
field.setAccessible(true)
val value = field.get(str).asInstanceOf[Array[Char]]
value(0) = 'W'
value(1) = 'o'
str // "Worst string"
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
def future1(): Future[Int] = ???
def future2(): Future[Int] = ???
val z1 = for {
x <- future1()
y <- future2()
} yield x + y
LongStream.range(0, users.size())
.mapToObj(this::createUserOrder)
.forEach(dao::save);
@sergiilagutin
sergiilagutin / integration.hs
Created March 9, 2016 17:58
Trapezoidal rule
integration :: (Double -> Double ) -> Double -> Double -> Double
integration f a b = let
step = (b-a)/1000
helper acc 0 x = acc
helper acc cnt x =
helper (acc + ((f x) + (f next))/2 * step) (cnt - 1) next
where next = x + step
in helper 0 1000 a
@sergiilagutin
sergiilagutin / custom-sorting.js
Created July 22, 2015 06:28
Custom sorting using datatables.js
// sort data in column using string representation on numeric values
// natural order: 1, 3, 2, 0
$(function () {
$.fn.dataTable.ext.order['my-sort'] = function (settings, col) {
return this.api().column(col, {order: 'index'}).nodes().map(function (td, i) {
var text;
switch ($(td).text()) {
case "0":
text = "zero";
@sergiilagutin
sergiilagutin / worksheet.scala
Created March 16, 2015 06:18
Simple reduce
import scala.annotation.tailrec
def sum(numbers: Seq[Int]): Int =
loopRight(numbers, 0, _ + _)
def product(numbers: Seq[Int]): Int =
loopRight(numbers, 1, _ * _)
def sumTail(numbers: Seq[Int]): Int =
loopLeft(numbers, 0, (acc, i) => acc + i)
@sergiilagutin
sergiilagutin / Runner.java
Created March 12, 2015 18:40
Force ConcurrentModificationException in single-thread application
import java.util.*;
List<String> list = new ArrayList<>();
list.add("one");
list.add("two");
for( String str : list) {
System.out.println(str);
list.add("three")
}
@sergiilagutin
sergiilagutin / Runner.java
Created March 11, 2015 11:06
Java creation of new object
class A {
static {
System.out.println("static A");
}
static {
System.out.println("static A2");
}
@sergiilagutin
sergiilagutin / Factorial.scala
Created March 9, 2015 06:29
Scala factorial using TDD
// step 1
object Factorial {
def fact(n: Int) =
if (n == 2) 2
else if (n == 5) 120
else 1
}
// step 2
object Factorial {
@sergiilagutin
sergiilagutin / Factorial.java
Created March 9, 2015 06:19
Java factorial using TDD
public class Factorial {
public int fact(int number) {
int i = 1;
int result = 1;
while (i <= number) {
result = result * i;
i++;
}
return result;
}