From: Richard W.M. Jones Date: Sun, 12 Jan 2020 19:56:36 +0000 (+0000) Subject: stdlib: Implement join() function. X-Git-Tag: v'0.2'~27 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=b29931c35b032141aaa8de370d9adbfc10705bf2;p=goals.git stdlib: Implement join() function. --- diff --git a/docs/Goalfile.pod b/docs/Goalfile.pod index 75ae4fc..9917fd0 100644 --- a/docs/Goalfile.pod +++ b/docs/Goalfile.pod @@ -38,6 +38,16 @@ For example: Returns the filename extension. +=head3 join (list1, list2) + +For example: + + join (["a", "b"], ["c", "d"]) ⇒ ["a", "b", "c", "d"] + +Concatenate C and C. It's not usually necessary to use +this function since goals automatically flattens lists within lists +into simple lists in many cases. + =head3 sort (list) For example: diff --git a/stdlib/prelude.gl b/stdlib/prelude.gl index d496d71..de2a422 100644 --- a/stdlib/prelude.gl +++ b/stdlib/prelude.gl @@ -54,6 +54,11 @@ pure function wrap (wrapper, xs) = @{ #---------------------------------------------------------------------- # Text functions. +# Join two lists. +pure function join (xs, ys) returning strings = @{ + for f in %xs %ys; do echo "$f"; done +} + # Sort + uniq a list. pure function sort (xs) returning strings = @{ for f in %xs; do echo "$f"; done | sort -u diff --git a/tests/10-function-join.expected b/tests/10-function-join.expected new file mode 100644 index 0000000..0ea1d24 --- /dev/null +++ b/tests/10-function-join.expected @@ -0,0 +1,5 @@ +x1 +x2 +y1 +y2 +y 3 diff --git a/tests/10-function-join.gl b/tests/10-function-join.gl new file mode 100644 index 0000000..345f79b --- /dev/null +++ b/tests/10-function-join.gl @@ -0,0 +1,25 @@ +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# 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 2 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Join two lists. + +let xs = [ "x1", "x2" ] +let ys = [ "y1", "y2", "y 3" ] +let r = join (xs, ys) + +goal all = { for x in %r; do echo "$x"; done } diff --git a/tests/10-function-join.sh b/tests/10-function-join.sh new file mode 100755 index 0000000..1df2719 --- /dev/null +++ b/tests/10-function-join.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# 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 2 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +set -e + +goals -f 10-function-join.gl > 10-function-join.out +diff -u 10-function-join.out 10-function-join.expected +rm 10-function-join.out