From 580298444204f92785a7ac78c4529e95e3cba40d Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sun, 12 Jan 2020 20:22:26 +0000 Subject: [PATCH 1/1] stdlib: Implement head() and tail() functions. --- docs/Goalfile.pod | 16 ++++++++++++++++ stdlib/prelude.gl | 17 +++++++++++++++++ tests/10-function-head.gl | 24 ++++++++++++++++++++++++ tests/10-function-head.sh | 24 ++++++++++++++++++++++++ tests/10-function-tail.expected | 3 +++ tests/10-function-tail.gl | 24 ++++++++++++++++++++++++ tests/10-function-tail.sh | 24 ++++++++++++++++++++++++ 7 files changed, 132 insertions(+) create mode 100644 tests/10-function-head.gl create mode 100755 tests/10-function-head.sh create mode 100644 tests/10-function-tail.expected create mode 100644 tests/10-function-tail.gl create mode 100755 tests/10-function-tail.sh diff --git a/docs/Goalfile.pod b/docs/Goalfile.pod index 40ca73a..37f3c07 100644 --- a/docs/Goalfile.pod +++ b/docs/Goalfile.pod @@ -38,6 +38,14 @@ For example: Returns the filename extension. +=head3 head (list) + +For example: + + head (["a", "b", "c"]) ⇒ "a" + +Returns the head (first) element of the list. + =head3 join (list1, list2) For example: @@ -76,6 +84,14 @@ This function works something like make’s C function, except that C is a regular expression, specifically a L extended regular expression. +=head3 tail (list) + +For example: + + tail (["a", "b", "c"]) ⇒ ["b", "c"] + +Returns the tail (all except first) elements of the list. + =head3 wildcard (pattern) For example: diff --git a/stdlib/prelude.gl b/stdlib/prelude.gl index 183338d..e2d0400 100644 --- a/stdlib/prelude.gl +++ b/stdlib/prelude.gl @@ -54,6 +54,14 @@ pure function wrap (wrapper, xs) = @{ #---------------------------------------------------------------------- # Text functions. +# Head of a list. +pure function head (xs) returning string = @{ + for f in %xs; do + echo "$f" + exit 0 + done +} + # Join two lists. pure function join (xs, ys) returning strings = @{ for f in %xs %ys; do echo "$f"; done @@ -71,6 +79,15 @@ pure function subst (from, to, text) returning string = @{ echo %text | sed -E s/%from/$to/g } +# Tail of a list. +pure function tail (xs) returning strings = @{ + drop=1 + for f in %xs; do + if [ -z "$drop" ]; then echo "$f"; fi + drop= + done +} + #---------------------------------------------------------------------- # File functions. diff --git a/tests/10-function-head.gl b/tests/10-function-head.gl new file mode 100644 index 0000000..3f94d08 --- /dev/null +++ b/tests/10-function-head.gl @@ -0,0 +1,24 @@ +# 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. + +# Test the head function. + +let xs = [ "a", "b", "c" ] +let x = head (xs) + +goal all = { echo %x } diff --git a/tests/10-function-head.sh b/tests/10-function-head.sh new file mode 100755 index 0000000..a7c49a9 --- /dev/null +++ b/tests/10-function-head.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-head.gl > 10-function-head.out +test "$(cat 10-function-head.out)" = "a" +rm 10-function-head.out diff --git a/tests/10-function-tail.expected b/tests/10-function-tail.expected new file mode 100644 index 0000000..6421689 --- /dev/null +++ b/tests/10-function-tail.expected @@ -0,0 +1,3 @@ +b +c c c +d d d diff --git a/tests/10-function-tail.gl b/tests/10-function-tail.gl new file mode 100644 index 0000000..9594db1 --- /dev/null +++ b/tests/10-function-tail.gl @@ -0,0 +1,24 @@ +# 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. + +# Test the head function. + +let xs = [ "a", "b", "c c c", "d d d" ] +let ys = tail (xs) + +goal all = { for f in %ys; do echo "$f"; done } diff --git a/tests/10-function-tail.sh b/tests/10-function-tail.sh new file mode 100755 index 0000000..2eb690f --- /dev/null +++ b/tests/10-function-tail.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-tail.gl > 10-function-tail.out +diff -u 10-function-tail.out 10-function-tail.expected +rm 10-function-tail.out -- 1.8.3.1