This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| LongStream.range(0, users.size()) | |
| .mapToObj(this::createUserOrder) | |
| .forEach(dao::save); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class A { | |
| static { | |
| System.out.println("static A"); | |
| } | |
| static { | |
| System.out.println("static A2"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // step 1 | |
| object Factorial { | |
| def fact(n: Int) = | |
| if (n == 2) 2 | |
| else if (n == 5) 120 | |
| else 1 | |
| } | |
| // step 2 | |
| object Factorial { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Factorial { | |
| public int fact(int number) { | |
| int i = 1; | |
| int result = 1; | |
| while (i <= number) { | |
| result = result * i; | |
| i++; | |
| } | |
| return result; | |
| } |
NewerOlder