juan_gandhi: (Default)
Джавасприпт, Хаскель и Скала, и, говорят, OCaml, из-за своей компактности, канают в качестве скриптовых языков.

Питон в этом смысле тоже ничо, но там, во-первых, споткнуться можно много где, во-вторых, на уровне компиляции нам ничего не скажут. Это уже на продакшене ебанется. То-то квора, которая на питоне, деплоится по сто раз в час (а тесты потом гоняют, и кодревью тоже). 
juan_gandhi: (VP)
function* foo(){
  var index = 0;
  while (index <= 2) // when index reaches 3, 
                     // yield's done will be true 
                     // and its value will be undefined;
    yield index++;
}
var iterator = foo();
println(iterator.next().value); // { value: 0, done: false }
println(iterator.next().value); // { value: 1, done: false }
println(iterator.next().value); // { value: 2, done: false }
println(iterator.next().value); // { value: undefined, done: true }
juan_gandhi: (VP)
Y = function (F) {
 var run = function (x) { return F(function (y) { return (x(x))(y)})}
 return run(run)
};
juan_gandhi: (VP)
/*
S combinator: \x \y \z x z (y z)
*/
window.S = function (x) { return function(y) { return function(z) { return x(z)(y(z)) } } };

/*
I combinator: \x x
*/
window.I = function(x) { return x };

/*
K combinator: \x \y x
*/
window.K = function(x) { return function() { return x }}

/*
Y combinator
*/
window.Y = Y = S (K(S(I)(I))) (S(S(K(S))(K)) (K(S(I)(I))));
juan_gandhi: (VP)
Только что в библиотеку добавил
window.K = function(x){return function(){return x}}
juan_gandhi: (VP)


<script>


function environmentName()
{
	var env = 'prod';
	var envCaps = env.toUpperCase();

	//alert("prod" + "  " + envCaps);
	return envCaps;
}
</script>


(repeated twice on each page)
juan_gandhi: (VP)
window.fold = function(list, zero, op) {
    var r = zero;
    for (var i = 0; i < list.length; i++) {
        r = op(r, list[i])
    }
    return r;
};

window.flatten = function(listOfLists) { 
    return fold(listOfLists, [], function(s1, s2) { return s1.concat(s2)})
};


Basically, I do map/reduce.
juan_gandhi: (VP)

window.project = (list, field) => map(list, (x) => x[field]);


I know, a list is supposed to have a map methods. But I don't observe it yet.
juan_gandhi: (VP)
Good stuff
https://github.com/lukehoban/es6features

var odds = evens.map(v => v + 1)

GET`http://foo.org/bar?a=${a}&b=${b}
    Content-Type: application/json
    X-Credentials: ${credentials}
    { "foo": ${foo},
      "bar": ${bar}}`(myOnReadyStateChangeHandler)

var [a, , b] = [1,2,3]

function f(x, y=12) { return x + y }
f(3) == 15

function f(x, ...y) { return x * y.length }
f(3, "hello", true) == 6

const x = "dont change me"

var arr = []
for (var i = 10; l--;) {
  let j = i
  result[i] = function() { return j }
}

arr[5]() === 5
arr[6]() === 6

var cat = 'Pavlova'
var dog = 'Charlie'
var pets = {cat, dog}

var s = new Set()
s.add("hello").add("goodbye").add("hello")
s.size === 2
s.has("hello") === true

var squares = [ x*x for (x of [1,2,3,4,5]) if (x % 2 == 1) ]

"abc".repeat(3).contains("cabca")

// tail call
function factorial(n, acc = 1) {
    'use strict';
    if (n <= 1) return acc;
    return factorial(n - 1, n * acc);
}
factorial(100000)

this one says "too much recursion
juan_gandhi: (VP)
Extracting PDF from Mozilla Browser - and Doing it Nicely

That's a detailed account of the thing I already posted a couple of days ago. And what I'm enjoying here is Functional Programming. I don't know why Stroustrup does not know that it works. Worked for me. :p
juan_gandhi: (VP)
	function changeLanguageOrder(){
		if(null!=document.documentElement.lang && null!=document.getElementById('selectedLanguage')){
			if(document.documentElement.lang=="es"){
				document.getElementById('selectedLanguage').options[1].selected=true;
			}
			else if(document.documentElement.lang=="ht"){
				document.getElementById('selectedLanguage').options[2].selected=true;
			}
			else if(document.documentElement.lang=="il"){
				document.getElementById('selectedLanguage').options[3].selected=true;
			}
			else if(document.documentElement.lang=="ko"){
				document.getElementById('selectedLanguage').options[4].selected=true;
			}
			else if(document.documentElement.lang=="tl"){
				document.getElementById('selectedLanguage').options[5].selected=true;
			}
			else if(document.documentElement.lang=="vi"){
				document.getElementById('selectedLanguage').options[6].selected=true;
			}
			else if(document.documentElement.lang=="zh" || document.documentElement.lang=="zt"){
				document.getElementById('selectedLanguage').options[7].selected=true;
			}
			else { 
				document.getElementById('selectedLanguage').options[0].selected=true;
			}
		}


A weird selection of languages, is not it?
juan_gandhi: (VP)
(from [livejournal.com profile] code_wtf)

In JavaScript, calculate the sum of integers in a nested array.

i.toSource().replace(/\[|\]/g, '').split(', ').forEach(function(a){if(a.match(/^\d$/)) s -= -a})

toSource()

Aug. 10th, 2013 09:39 am
juan_gandhi: (VP)
В джаваскрипте есть ценный метод, toSource(); он, правда, только к функциям применяется - но я тут понарисовал для скалы, чтобы вот взял какую-нибудь структуру, и её в лог гонишь в таком формате, что можно потом прямо в юниттест вставлять, удобно ж.


List(List("Claim Number:", "19130723010200", "Date Paid:", "", "Date of Claim:", "07/16/1913", "Total Charges: 1", "1", "£1,350.00", "End Service Date:", "07/16/1913", "Plan Paid: 1", "1", "", "Status:", "IN REVIEW", "You Pay: 1", "1", "", "Member:", "LEOPOLD BLOOM", "", "", "Member ID:", "XXXXXXXXX-01", "", ""))

a prayer

May. 8th, 2013 05:39 pm
juan_gandhi: (VP)
can anybody please please please add val to JavaScript?!!!!!

fun/fuck

Sep. 9th, 2012 11:45 pm
juan_gandhi: (Default)
JavaScript/Scala
Read more... )

new stuff

Aug. 14th, 2012 12:01 pm
juan_gandhi: (Default)
function New(name) { return java.lang.Class.forName(name).newInstance() }

function newList() { return New("java.util.LinkedList") }

scala->js

Aug. 9th, 2012 01:08 pm
juan_gandhi: (Default)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function arrayFrom(list) {
  var out = [];
  for (i = 0; i < list.size(); i++) { out[i] = asJS(list.apply(i)) }
  return out;
}

function mapFrom(map) {
  var out = {};

  var i = map.iterator();
  while(i.hasNext()) {
    var e = i.next();
    out[e._1()] = asJS(e._2())
  }
  return out;
}


function isListlike(name) {
  return name.indexOf("List") >= 0 || name.indexOf("Array") >= 0 || name.indexOf("$colon$colon") >= 0
}

function isMapLike(name) {
  return name.indexOf("Map") >= 0
}

function asJS(obj) {
  try {
    var cn = obj.getClass().getSimpleName();
    if (isListlike(cn)) return arrayFrom(obj);
    if (isMapLike(cn))  return mapFrom(obj);
  } catch (x) {}
  return obj;
}


critique?

Profile

juan_gandhi: (Default)
juan_gandhi

October 2017

S M T W T F S
1 2 3 45 6 7
8 910 11 12 13 14
15 16 17 18 19 20 21
22232425262728
293031    

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Oct. 22nd, 2017 06:18 am
Powered by Dreamwidth Studios