From 6049de701cea350b3f474e4c1ff16836baa8e2fb Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sun, 12 Jan 2020 20:48:29 +0000 Subject: [PATCH] stdlib: Implement filter() and filter-out() functions. --- docs/Goalfile.pod | 18 ++++++++++++++++++ stdlib/prelude.gl | 10 ++++++++++ tests/10-function-filter-out.expected | 4 ++++ tests/10-function-filter-out.gl | 23 +++++++++++++++++++++++ tests/10-function-filter-out.sh | 24 ++++++++++++++++++++++++ tests/10-function-filter.expected | 3 +++ tests/10-function-filter.gl | 23 +++++++++++++++++++++++ tests/10-function-filter.sh | 24 ++++++++++++++++++++++++ 8 files changed, 129 insertions(+) create mode 100644 tests/10-function-filter-out.expected create mode 100644 tests/10-function-filter-out.gl create mode 100755 tests/10-function-filter-out.sh create mode 100644 tests/10-function-filter.expected create mode 100644 tests/10-function-filter.gl create mode 100755 tests/10-function-filter.sh diff --git a/docs/Goalfile.pod b/docs/Goalfile.pod index 85d19ba..9031780 100644 --- a/docs/Goalfile.pod +++ b/docs/Goalfile.pod @@ -46,6 +46,24 @@ For example: Returns the filename extension. +=head3 filter (pattern, list) + +For example: + + filter ("a+", ["a", "b", "ca"]) ⇒ ["a", "ca"] + +Filter a list returning only the elements that match the extended +regular expression C. + +=head3 filter-out (pattern, list) + +For example: + + filter-out ("a+", ["a", "b", "ca"]) ⇒ ["b"] + +Filter a list returning only the elements that I match the +extended regular expression C. + =head3 head (list) For example: diff --git a/stdlib/prelude.gl b/stdlib/prelude.gl index e8a96e8..d8e523a 100644 --- a/stdlib/prelude.gl +++ b/stdlib/prelude.gl @@ -59,6 +59,16 @@ pure function wrap (wrapper, xs) = @{ #---------------------------------------------------------------------- # Text functions. +# Filter a list by regexp. +pure function filter (pat, xs) returning strings = @{ + for f in %xs; do echo "$f"; done | grep -E -- %pat +} + +# Filter out a list by regexp. +pure function filter-out (pat, xs) returning strings = @{ + for f in %xs; do echo "$f"; done | grep -v -E -- %pat +} + # Head of a list. pure function head (xs) returning string = @{ for f in %xs; do diff --git a/tests/10-function-filter-out.expected b/tests/10-function-filter-out.expected new file mode 100644 index 0000000..9d1327a --- /dev/null +++ b/tests/10-function-filter-out.expected @@ -0,0 +1,4 @@ +b + +bbb +ddd diff --git a/tests/10-function-filter-out.gl b/tests/10-function-filter-out.gl new file mode 100644 index 0000000..904bb30 --- /dev/null +++ b/tests/10-function-filter-out.gl @@ -0,0 +1,23 @@ +# 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 filter function. + +let xs = filter-out ("a+", [ "b", "", "aa", "abc", "bca", "bbb", "ddd" ]) + +goal all = { for x in %xs; do echo "$x"; done } diff --git a/tests/10-function-filter-out.sh b/tests/10-function-filter-out.sh new file mode 100755 index 0000000..a550def --- /dev/null +++ b/tests/10-function-filter-out.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-filter-out.gl > 10-function-filter-out.out +diff -u 10-function-filter-out.out 10-function-filter-out.expected +rm 10-function-filter-out.out diff --git a/tests/10-function-filter.expected b/tests/10-function-filter.expected new file mode 100644 index 0000000..f022ad4 --- /dev/null +++ b/tests/10-function-filter.expected @@ -0,0 +1,3 @@ +aa +abc +bca diff --git a/tests/10-function-filter.gl b/tests/10-function-filter.gl new file mode 100644 index 0000000..f660c2d --- /dev/null +++ b/tests/10-function-filter.gl @@ -0,0 +1,23 @@ +# 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 filter function. + +let xs = filter ("a+", [ "b", "", "aa", "abc", "bca", "bbb", "ddd" ]) + +goal all = { for x in %xs; do echo "$x"; done } diff --git a/tests/10-function-filter.sh b/tests/10-function-filter.sh new file mode 100755 index 0000000..f8e1bdb --- /dev/null +++ b/tests/10-function-filter.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-filter.gl > 10-function-filter.out +diff -u 10-function-filter.out 10-function-filter.expected +rm 10-function-filter.out -- 1.8.3.1