vtm/quelldatein/helfer/fehlermonade.ts

136 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-03-28 11:19:46 +02:00
/*
* Verrückte Turing-Maschinen A turing complete game
* Copyright (C) 2016 Christian Fraß <vidofnir@folksprak.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-03-28 13:59:29 +02:00
module lib_errormonade
2018-03-28 11:19:46 +02:00
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export type type_errormonade<type_value> = lib_call.type_complex<any>;
2018-03-28 11:19:46 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function create_nothing<type_value>
2018-03-28 11:19:46 +02:00
(
)
2018-03-28 13:59:29 +02:00
: type_errormonade<type_value>
2018-03-28 11:19:46 +02:00
{
return (
2018-03-28 13:59:29 +02:00
lib_call.wrap<any>
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
"nothing",
2018-03-28 11:19:46 +02:00
{
}
)
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function create_just<type_value>
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
value : type_value
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: type_errormonade<type_value>
2018-03-28 11:19:46 +02:00
{
return (
2018-03-28 13:59:29 +02:00
lib_call.wrap<any>
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
"just",
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
"value": value
2018-03-28 11:19:46 +02:00
}
)
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function filled<type_value>
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
errormonade : type_errormonade<type_value>
2018-03-28 11:19:46 +02:00
)
: boolean
{
return (
2018-03-28 13:59:29 +02:00
lib_call.distinguish<any>
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
errormonade,
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
"nothing": ({}) => false,
"just": ({"value": value}) => true,
2018-03-28 11:19:46 +02:00
}
)
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function read<type_value>
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
errormonade : type_errormonade<type_value>
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: type_value
2018-03-28 11:19:46 +02:00
{
return (
2018-03-28 13:59:29 +02:00
lib_call.distinguish<any>
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
errormonade,
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
"nothing": ({}) => {throw (new Error("read von nothing"));},
"just": ({"value": value}) => value,
2018-03-28 11:19:46 +02:00
}
)
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function propagate<type_value1, type_value2>
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
errormonade : type_errormonade<type_value1>,
funktion : (value1 : type_value1)=>type_errormonade<type_value2>
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: type_errormonade<type_value2>
2018-03-28 11:19:46 +02:00
{
return (
2018-03-28 13:59:29 +02:00
lib_call.distinguish<any>
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
errormonade,
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
"nothing": ({}) => create_nothing<type_value2>(),
"just": ({"value": value1}) => funktion(value1),
2018-03-28 11:19:46 +02:00
}
)
);
}
}