<commit_before><?php
//This class represents a value or an error. Inspiration is taken from the haskell Maybe/Either classes.
class Maybe {
	private function __construct() {}
	private $val;
	private $error;
	private  $isError;

	public static function just($val) {
		$instance = new Maybe;
		$instance->val= $val;
		$instance->error=null;
		$instance->isError=false;
		return $instance;
	}
	public static function error($msg) {
		$instance = new Maybe;
		$instance->val= null;
		$instance->error=$msg;
		$instance->isError=true;
		return $instance;
	}

	public function e() {
		return $this->isError; 
	}
	public function v() {
		return $this->val;
	}
	public function m() {
		return $this->error;
	}
}

//This class implements a monadic bind function for Maybe. Inspiration again taken from haskell.
//Using bind instead of function normal function application, makes is possible to treat Maybe's 
//as normal values.
function bind($func, Maybe $maybeVal) {
	if($maybeVal->e()) {
		return $maybeVal;
	} else {
		return $func($maybeVal->v());
	}
}

//Since there is no currying in php, i think that i will have to do this for every version of bind
//that i am going to use. Also i have realized that this in fact is more like the Writer monad from
//haskell, i don't think that it will be possible to concat the errors. If it is, then it would
//atleast be neccesary to change maybe so it represents a function that might fail, instead of
//the result of a function that might fail. Im not really sure if this is possible in php.
//for now i just pass on the first error.
function bind3($func, Maybe $v1, Maybe $v2, Maybe $v3) {
	if($v1->e()) {
		return $v1;
	} elseif($v2->e()) {
		return $v2;
	} elseif($v3->e()){
		return $v3;
	} else {
		return $func($v1->v(), $v2->v(), $v3->v());
	}
}

function mreturn($val) {return Maybe::just($val);}

function liftM($func) {
	$r = function($a1) use ($func) {
		return Maybe::just($func($a1));
	};
	return $r;
}

function liftM3($func) {
	$r = function($a1, $a2, $a3) use ($func) {
		return Maybe::just($func($a1, $a2, $a3));
	};
	return $r;
}
?>
<commit_msg>Clean maybe.php up and Add bind2 function.<commit_after><?php
/*This class represents the result of a function that might fail.
Inspiration is taken from the Maybe/Either types in haskell.
But it is not strictly a copy of any of them.
It is however still a monad. Since i have implemented both bind and return.*/
class Maybe {
	private function __construct() {}
	private $val;
	private $error;
	private  $isError;

	public static function just($val) {
		$instance = new Maybe;
		$instance->val= $val;
		$instance->error=null;
		$instance->isError=false;
		return $instance;
	}
	public static function error($msg) {
		$instance = new Maybe;
		$instance->val= null;
		$instance->error=$msg;
		$instance->isError=true;
		return $instance;
	}

	public function e() {
		return $this->isError; 
	}
	public function v() {
		return $this->val;
	}
	public function m() {
		return $this->error;
	}
}

/*The bind family of functions makes it possible to treat Maybe's as
normal values, with automated handling of any error. The function
application syntas becomes quite weird though:
bind('f', x); instead of f(x);*/
function bind($func, Maybe $maybeVal) {
	if($maybeVal->e()) {
		return $maybeVal;
	} else {
		return $func($maybeVal->v());
	}
}
function bind2($func, Maybe $v1, Maybe $v2) {
	if($v1->e()) {
		return $v1;
	} elseif($v2->e()) {
		return $v2;
	} else {
		return $func($v1->v(), $v2->v());
	}
}
function bind3($func, Maybe $v1, Maybe $v2, Maybe $v3) {
	if($v1->e()) {
		return $v1;
	} elseif($v2->e()) {
		return $v2;
	} elseif($v3->e()){
		return $v3;
	} else {
		return $func($v1->v(), $v2->v(), $v3->v());
	}
}
/*Takes a normail value and turns it into a Maybe value */
function mreturn($val) {return Maybe::just($val);}

/*Promotes a normal function to a Maybe function*/
function liftM($func) {
	$r = function($a1) use ($func) {
		return Maybe::just($func($a1));
	};
	return $r;
}
function liftM3($func) {
	$r = function($a1, $a2, $a3) use ($func) {
		return Maybe::just($func($a1, $a2, $a3));
	};
	return $r;
}
?>

{'fraction_non_alphanumeric': 0.1578564500484966, 'fraction_numerical': 0.013094083414161009, 'mean_word_length': 3.9107142857142856, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3765060', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1443, 'n_words': 496}