Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Lambdas bash extended

By: a guest on May 9th, 2014  |  syntax: Bash  |  size: 0.87 KB  |  views: 309  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. lambda()
  2. {
  3.         # Save orignal function
  4.         local ORIGINAL=$(declare -f otl)
  5.         local SHIFT_SIZE=${1}
  6.         shift
  7.         local vars=()
  8.  
  9.         for ((i=0;i<${SHIFT_SIZE};i++)); do
  10.                 vars+=(${1})
  11.                 shift
  12.         done
  13.  
  14.         eval "otl() { $@; }"
  15.         otl ${vars[@]}
  16.         unset -f otl
  17.         eval "${ORIGINAL}"
  18. }
  19.  
  20.  
  21. accept_lambda()
  22. {
  23.     declare -a argAry1=("${!1}")
  24.     echo "${argAry1[@]}"
  25.         lambda ${argAry1[@]}
  26. }
  27.  
  28.  
  29. test_passing() {
  30.         echo "Test passing lambda"
  31.         local lamdef=(3 "test" "lambda" "pass" 'echo $3 $2 $1')
  32.         echo "Passing ${lamdef[@]}"
  33.         accept_lambda lamdef[@]
  34. }
  35.  
  36. returns_callback(){
  37.         lamdef=(3 test call back 'echo $3 $2 $1')
  38.         echo "${lamdef[@]}"
  39. }
  40.  
  41. test_callbacks(){
  42.         echo "Test callback"
  43.         local lamdef=($(returns_callback))
  44.         echo "Got callback: ${lamdef[@]}"
  45.         lambda ${lamdef[@]}
  46. }
  47.  
  48.  
  49. echo "Test lambda call"
  50. lambda 3 ro sham bo 'echo $3 $2 $1'
  51.  
  52.  
  53. test_passing
  54. test_callbacks
clone this paste RAW Paste Data