From 692f7e7a70572c5f4f7242ce721a674a75b34e06 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sun, 12 Jan 2020 21:07:57 +0000 Subject: [PATCH] stdlib: Implement last() and nth() functions. --- docs/Goalfile.pod | 16 ++++++++++++++++ stdlib/prelude.gl | 19 +++++++++++++++++++ tests/10-function-last.gl | 24 ++++++++++++++++++++++++ tests/10-function-last.sh | 24 ++++++++++++++++++++++++ tests/10-function-nth.gl | 24 ++++++++++++++++++++++++ tests/10-function-nth.sh | 24 ++++++++++++++++++++++++ 6 files changed, 131 insertions(+) create mode 100644 tests/10-function-last.gl create mode 100755 tests/10-function-last.sh create mode 100644 tests/10-function-nth.gl create mode 100755 tests/10-function-nth.sh diff --git a/docs/Goalfile.pod b/docs/Goalfile.pod index 9031780..f586ec4 100644 --- a/docs/Goalfile.pod +++ b/docs/Goalfile.pod @@ -82,6 +82,22 @@ 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 last (list) + +For example: + + last (["a", "b", "c"]) ⇒ "c" + +Returns the last element of a list. + +=head3 nth (n, list) + +For example: + + nth (1, ["a", "b", "c"]) ⇒ "b" + +Returns the n’th element of a list (counting from 0). + =head3 read (filename) For example: diff --git a/stdlib/prelude.gl b/stdlib/prelude.gl index d8e523a..13ac311 100644 --- a/stdlib/prelude.gl +++ b/stdlib/prelude.gl @@ -82,6 +82,25 @@ pure function join (xs, ys) returning strings = @{ for f in %xs %ys; do echo "$f"; done } +# Last element of a list. +pure function last (xs) returning string = @{ + for f in %xs; do + r="$f" + done + echo "$r" +} + +# n'th element of a list. +pure function nth (n, xs) returning string = @{ + i=0 + r= + for f in %xs; do + if [ $i -eq %n ]; then r="$f"; fi + ((++i)) + done + echo "$r" +} + # 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-last.gl b/tests/10-function-last.gl new file mode 100644 index 0000000..c9a1e55 --- /dev/null +++ b/tests/10-function-last.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 last function. + +let xs = [ "a", "b", "c" ] +let x = last (xs) + +goal all = { echo %x } diff --git a/tests/10-function-last.sh b/tests/10-function-last.sh new file mode 100755 index 0000000..2ca6784 --- /dev/null +++ b/tests/10-function-last.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-last.gl > 10-function-last.out +test "$(cat 10-function-last.out)" = "c" +rm 10-function-last.out diff --git a/tests/10-function-nth.gl b/tests/10-function-nth.gl new file mode 100644 index 0000000..87eedd9 --- /dev/null +++ b/tests/10-function-nth.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 nth function. + +let xs = [ "a", "b", "c" ] +let x = nth ("1", xs) + +goal all = { echo %x } diff --git a/tests/10-function-nth.sh b/tests/10-function-nth.sh new file mode 100755 index 0000000..845c686 --- /dev/null +++ b/tests/10-function-nth.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-nth.gl > 10-function-nth.out +test "$(cat 10-function-nth.out)" = "b" +rm 10-function-nth.out -- 1.8.3.1