That’s crazy cool.
(Source: ckvey)
I enjoy functional programming and cookies.
Kurry - A JavaScript library containing various types of currying functions.
A little library I wrote a few weeks ago.
I am currently attempting to design a system involving:
These are the main features of a project idea I’ve had for a while, which has recently become rather more colourful.
I originally wanted to try to build a generic pattern matching system with a new type of regular expression for almost any data type: (lists, trees, graphs, …). Similar to clojure’s core.match.
Recently I’ve been having ideas about CSS: how terrible it is at it’s job, and how I would design a replacement.
It turns out that both of these projects have similar components and ideas have crossed between them; so much so that now the projects have merged into one!
This project touches on a few computing science areas that peek my interest: programming languages and semantics, logic programming, functional programming, and pattern matching.
I still have a lot to work out, but it should be interesting and fun :]
Forcing super() to be the first call in a subclass in java is really restricting.
I need to process these parameters :c.
I mean, it’s doable if you make subroutines that do it, but I’d rather just do it in the constructor and not bloat this class with useless one-off private methods.
What are you working on?
Arguably, the constructor shouldn’t be processing the data or delegating the processing to a one-off method; a factory method might make more sense. So the constructor assumes the data it gets is already in a valid state.
Over the weekend I took a short tour of the Scala language. There were many aspects of the language I found pleasing. One such feature was the use of the underscore identifier to produce partially applied functions.
object Main extends App {
def f(a:Int, b:Int, c:Int):List[Int] = a::b::c::Nil
// g = partially applied f, supplying only the 2nd argument
val g:(Int,Int) => List[Int] = f(_, 2, _)
// Now apply with the 1st and 3rd arguments
val result = g(3, 1)
// Prints true
System.out.println(result == List(3, 2, 1))
}
I think this is a very nice method of partial application, so I tried to implement something similar in JavaScript (because why not?).
Here’s how to use it:
var f = function (a, b, c) { return [a, b, c]; };
var g = __.part(f)(__, 2, __);
var result = g(3, 1);
console.log(result[0] === 3); // true
console.log(result[1] === 2); // true
console.log(result[2] === 1); // true
Here’s the source:
var __ = {
part: function (fn) {
return function () {
var _args = arguments;
return function () {
var nArgs = arguments;
var args = [];
var index = 0;
for (var i = 0, m = _args.length; i < m; i += 1) {
var elem =_args[i];
args.push(
( elem === __ ? nArgs[index++]
: elem === __.copy ? __
: elem ));
}
return fn.apply(void 0,
args.concat(
[].slice.call(nArgs, index)));
};
};
},
copy: {}
};
Object.freeze && Object.freeze(__);
I used a double underscore instead of a single as to not conflict with the underscore.js library.
Also note the __.copy object: this is for if you want to supply __ as an actual argument to a partial application:
// third argument is actually the __ object
func(__, __, __.copy)
Each __ get replaced with arguments supplied in the subsequent applications, and __.copy gets replaced with __.
- Learn SQL
- Learn C++11
- Punch Logan
- Learn Java
- Try and Mod Minecraft?
- Punch Chris
- Learn SFML
- Hire a prostitute
- Learn as many other things as possible.
Fuck yeah. I rank higher than Chris.
Suck it, Chris.
Nice list! Here’s mine :]
For this Java thing, I want a pseudo-constructor thing to make sure that when I have an object created from the name “foo”, it’s the same object as all other objects created from that name. Here’s what I’m using right now:
import java.util.HashMap; public class Item { private static...
This is how I might go about implementing it: http://pastebin.com/v0A5KjqR
What do you think?
I don’t think I’ll ever understand how closures work internally.
Function pointers, and lots of black magic.
They should have a fairly straight forward implementation. Function data would be associated with a reference to the scope stack it’s built in. Upon procedure application, a new scope is pushed on to the function’s referenced stack for the declaration of new variables. If a function expression is evaluated within this scope, it creates function data that is associated with a reference to the current scope stack. Multiple executions of the procedure would yield multiple function data each with the same scope stack tail, just with different heads.
(via cjbrowne)
What most schools don’t teach (by CodeOrg)
こういうコンセプト好き!
This is the biggest load of propaganda I have ever seen pertaining to programming. Programming isn’t about free food, dragging and dropping pictures, or playing video games. It’s a career. Some people are interested in it, some people are not. But dear God stop making it look like it’s the easiest career in the world and be a little more realistic about it.
Firstly, the reason why it pays so well and is so in demand compared with supply is that it’s DIFFICULT. If everyone could sit on their butts and play pong or watch TV for that much money, don’t you think we would have an adequate supply of “programmers”?
Second, if you’re going to convince people to go into computer science because it’s fun, deliver on that promise. Start reforming curriculums to accept inexperienced but motivated young people, make the projects relatable, and encourage peer interaction in the classroom. There are many, many schools that cannot even handle this part.
Honestly? Not everybody needs to learn to code. I’m all for educating people on what coding is and giving them the option to explore how it could help them, but please cut it out with this “To live in this society you need to learn to code” because it makes you look like an arrogant, pretentious douchebag.
Some of these people talking aren’t even barely qualified to talk about it. They were just famous and one had even only “taken a programming class”.
Reblog for the great comment ^
(via meghabits)
Sublime Text 2 Plugin: Simplifies writing DocBlock comments in JavaScript, PHP, CoffeeScript, Actionscript, C & C++
So I just took an online programming test, and I got 60 out of 100.
Wha…
I took this three times lol. The first time I got 40, second 47, then 100! ^-^
Here’s my final JavaScript solution:
function equi(A) {
var len = A.length;
var index = 0;
var leftTotal = 0;
for (; index < len - 1; index += 1) {
leftTotal += A[index];
}
index = len - 1;
var rightTotal = 0;
var item = A[index];
while (index >= 0) {
if (leftTotal === rightTotal) {
break;
}
rightTotal += item;
index -= 1;
item = A[index];
leftTotal -= item;
}
return index;
}
My first two solutions were pretty terrible, mostly because I was trying to rush without really thinking about what I was supposed to be doing.
A 41 character JavaScript expression that evaluates to true if the code has been minified (or rather; if it has had the comments stripped):
!function f(){/**/return/\*\//.test(f)}()
Edit: A shorter one at 37 characters:
!function f(){ return/\sr/.test(f)}()
This one detects if the space has been removed before the return statement.
anyway i wrote a macro
macros['link'] = { handler: function (place, macroName, params, parser) { var link = document.createElement('a'); var name = eval("state.history[0].variables." + params[0].substr(1)); link.href = 'javascript:void(0)'; link.className = 'internalLink link'; if (params[1])...
Eval is considered bad practice! How about something like this?
(function (undefined) { "use strict";
/* Constants */
var VOID_URL = 'javascript:void(0)';
var LINK_CLASSES = 'internalLink link';
/* Some Handy Dandy Utility Functions */
/**
* walkPath :: Object -> String -> ( a | Undefined )
*
* Given an object, and a path, walk the path through the
* object and retrieve the final value.
* If at any point we cannot progress, the result is Undefined.
* Can also walk arrays:
* walkPath({a:[{b:5}]})('a.0.b') === 5
*/
var walkPath = function (object) {
return function (path) {
var result = object, segments = path.split('.');
for (var i = 0, m = segments.length; i < m; i += 1) {
try { result = result[segments[i]]; }
catch(e) { return undefined; }
}
return result;
};
};
/**
* getName :: String -> ( a | Undefined )
*
* Returns the value for a path in `state.history[0].variables`
* or Undefined.
*
* This assumes that the `...variables` object is already defined.
* If variables isn't yet defined, use a lazier version:
* var getName = function (path) { return walkPath(state.history[0].variables)(path); };
*/
var getName = walkPath(state.history[0].variables);
/* Macro Functions */
macros.link = {
handler: function (place, macroName, params, parser) {
var name = getName(params[0].substr(1));
var link = document.createElement('a');
link.href = VOID_URL;
link.className = LINK_CLASSES;
link.innerHTML = params[1] || name;
link.addEventListener('click', function () {
state.display(name, link);
});
place.appendChild(link);
}
};
macros.vardisplay = {
handler: function (place, macroName, params, parser) {
var name = getName(params[0].substr(1));
console.log('<<vardisplay>>ing $' + params[0] + '/"' + name + '"');
new Wikifier(place, tale.get(name).text);
console.log('<<vardisplay>> of "' + params[0] + '" complete');
}
};
})();
Some changes you may notice:
// These are equivalent:
if (params[1]) { link.innerHTML = params[1]; } else { link.innerHTML = name; }
link.innerHTML = params[1] ? params[1] : name;
link.innerHTML = params[1] || name;
// element.addEventListener(event, callback) is nice
Only a good programmer can understand this.
- The code listed here actually doesn’t semantically make any fucking sense. A character which is supposed to be the programmer’s name is being read after a comparison is made using this value. (Or lack thereof)
- This isn’t even valid syntax. Brackets can be omitted only on single-line statements in if/else statements. Additionally, brackets can be used to create scoped closures anywhere, but seriously, what the fuck?
- This is embarrassing.
- You should be embarrassed.
- I bet you paid money for this.
A tokenizer and parser could be written to accept programs written in this syntax, and a compiler/interpreter could be written to give semantics to said language. Indentation could be used to denote code blocks. An if + else statement could be enclosed in curley braces (`{if`…`else`…`}`). Could have a dynamic type system. Etc…
Although there (probably) doesn’t exist a definition of this language, it could be done. I could make up a language and give it the same semantics as the program listed on the tee-shirt:
!{NEVERMIND.}_[!{"You're INVULNERABLE!"}?{YOU:K%}]
-:Given YOU ^ PROGRAMMER
Problem description: Sum to Zero
You are given an array of integers. Count the numbers of ways in which the sum of 4 elements in this array results in zero.
Descriptive Haskell solution:
-- Cons a value onto the beginning -- of every element in a list (•) = map . (:) -- Find the combinations of a list combs = foldr (\y vs -> vs ++ (y • vs)) [[]] -- Given a number k and a list vs -- The length of vs must equal k -- And the sum of vs must equal 0 predicate k vs = (length vs == k) && (sum vs == 0) -- Find the combinations of a list -- Filtered by the predicate filtered_combs k = filter (predicate k) . combs -- Count the number of filtered combinations process k = length . filtered_combs k
MONOLITHIC Haskell version
cthulhu k = length . filter (\xs -> length xs == k && sum xs == 0) . foldr (\y vs -> vs ++ (map (y:) vs)) [[]]
I’m aware that there is likely a more efficient way of computing this – in which only those combinations that are of the correct length are computed – maybe I’ll work on that next…