Commits

patrickl  committed 6eb140b

[svn r544] ok

  • Participants
  • Parent commits 9e0123a

Comments (0)

Files changed (4)

 PACKAGE=bashkell
-PACKAGE_REV=1.01
+PACKAGE_REV=1.02
 PACKAGE_REL=1
 MANDIR=/usr/share/man
 ARCH=noarch
+- Document map -s
 #!/bin/bash
 
 
-__version='1.01'
+__version='1.02'
 
 
 OPTIND=
 function map {
 	OPTIND=
 	local __rc=
-	__check_optr $0 "$@"
+	local __split=
+	local __opt
+	while getopts ":rs" __opt ; do
+		case $__opt in
+			r) __rc=1 ;;
+			s) __split=1 ;;
+			*) __die "$0: unrecognized option -$OPTARG" ;;
+		esac
+	done
 	shift $(($OPTIND - 1))
-	__check_usage "map [-r] EXPRESSION" "$@" || return 1
+	__check_usage "map [-r] [-s] EXPRESSION" "$@" || return 1
 
-	local __f=$(__make_lambda 1 "$1")
+	local __c=1
+	local __nbl=1
+	local __ml_opt=
+	if [ -n "$__split" ] ; then
+		__nbl=0
+		__ml_opt='-c'
+	fi
+	local __f=$(__make_lambda $__ml_opt $__nbl "$1")
+	if [ -n "$__split" ] ; then
+		__c=${__f##* }
+		__f=${__f% *}
+	fi
 	shift
 
 	__args2stdin "$@" | {
 		local __redir=
 		[[ -n "$__rc" ]] && __redir=">/dev/null"
 
-		local __a
-		while read -r __a ; do
-			local __lambda1="$__a"
+		local __names=$(__make_names $__c)
+		local __name
+		for __name in $__names ; do
+			eval "local $__name"
+		done
+		while read -r $__names ; do
 			eval "$__f $__redir"
 			local __r="$?"
 			[[ -n "$__rc" ]] && echo "$__r"
 function foldl {
 	OPTIND=
 	local __rc=
-	__check_optr $0 "$@"
+	local __opt
+	while getopts ":rs" __opt ; do
+		case $__opt in
+			r) __rc=1 ;;
+			*) __die "$0: unrecognized option -$OPTARG" ;;
+		esac
+	done
 	shift $(($OPTIND - 1))
 	__check_usage "foldl [-r] EXPRESSION ACC" "$@" || return 1
 
 function foldr {
 	OPTIND=
 	local __rc=
-	__check_optr $0 "$@"
+	local __opt
+	while getopts ":rs" __opt ; do
+		case $__opt in
+			r) __rc=1 ;;
+			*) __die "$0: unrecognized option -$OPTARG" ;;
+		esac
+	done
 	shift $(($OPTIND - 1))
 	__check_usage "foldr [-r] EXPRESSION ACC" "$@" || return 1
 
 	filter '\x -> __n=$((__n-1)) ; ((__n >= 0))' "$@"
 }
 
+
 function drop {
 	__check_usage "drop N" "$@" || return 1
 	local __n="$1"
 }
 
 
+# Debug function.
+function __debug {
+	if [ -n "$BASHKELL_DEBUG" ] ; then
+		echo -n "DEBUG: " >&2
+		echo "$@" >&2
+	fi
+}
+
+
 # Exits with the argument as return code.
 # Feels more efficient than (exit $1).
 function __return {
 
 
 function __make_lambda {
+	OPTIND=
+	local __count=
+	local __opt=
+	while getopts "c" __opt ; do
+		case $__opt in
+			c) __count=1 ;;
+		esac
+	done
+    shift $(($OPTIND - 1))
+
 	local __n="$1"
 	local __f="$2"
 
 				__i=$((__i + 1))
 			done
 
-			echo "$__f" 
+			echo -n "$__f" 
+			[ -n "$__count" ] && echo -n " $__i"
+			echo
 		else
 			__die "lambda syntax error near '$__f'"
 		fi
 }
 
 
-function __check_optr {
-	local name="$1"
-	shift 
-
-	# Here rc is global, is is defined local on the caller.
-	__rc=
-	local opt
-	while getopts ":r" opt ; do
-		case $opt in
-			r) __rc=1 ;;
-			*) __die "$name: unrecognized option -$OPTARG" ;;
-		esac
-	done ;
+function __make_names {
+	local n=$1
+	local i
+	for ((i=1 ; i<=n ; i++)) ; do
+		echo -n "__lambda$i "
+	done
+	echo
 }
 
 
 source ../bashkell
 
 
-plan_tests 13
+plan_tests 14
 
 
 is "`map basename $ t/data/file1.txt`" 'file1.txt' \
 is "`map basename $ t/data/file1.txt | map ' \ a -> echo ${a#f}'`" 'ile1.txt' \
 	"map with lambda (5)"
 
+is "`map -s '\a b -> echo $a,$b' $ 'a b c' 'd e f'`" "`echo -e "a,b\nd,e"`" \
+	"map with -s"
+
 is "`map -r echo $ a b c`" "`echo -e "0\n0\n0"`" \
 	"map with -r (1)"
 is "`map -r /bin/false $ a`" "`echo -e "1"`" \