8 kyu

The 'if' function

14,201 of 34,190user578387

Description:

Create a function called _if which takes 3 arguments: a value bool and 2 functions (which do not take any parameters): func1 and func2

When bool is truthy, func1 should be called, otherwise call the func2.

Example:

_if(true, (() -> console.log 'true'), (() -> console.log 'false'))
// Logs 'true' to the console.
void the_true() { printf("true"); }
void the_false() { printf("false"); }
_if(true, the_true, the_false);
/* Logs "true" to the console */
the_true:
    mov rdi .true
    jmp printf
.true:   db  `true\0`

the_false:
    mov rdi, .false
    jmp printf
.false:  db  `false\0`

mov dil, 1
mov rsi, the_true
mov rdx, the_false
call _if            ; Logs "true" to the console
void TheTrue() { std::cout << "true" }
void TheFalse() { std::cout << "false" }
_if(true, TheTrue, TheFalse);
// Logs 'true' to the console.
Kata.If(true, () => Console.WriteLine("True"), () => Console.WriteLine("False"));
// write "True" to console
_if(true, fn -> IO.puts "true" end, fn -> IO.puts "false" end)
# prints "true" to the console
main = _if True (putStrLn "You spoke the truth") (putStrLn "liar")
-- puts "You spoke the truth" to the console.

_if False "Hello" "Goodbye" -- "Goodbye"
_if(true, function(){console.log("True")}, function(){console.log("false")})
// Logs 'True' to the console.
_if(true, proc{puts "True"}, proc{puts "False"})
# Logs 'True' to the console.
def truthy(): 
  print("True")
  
def falsey(): 
  print("False")
  
_if(True, truthy, falsey)
# prints 'True' to the console
_if(true, || println!("true"), || println!("false"))
# prints "true" to the console
kata._if(true, function() print("true") end, function() print("false") end)
-- prints "true" to the console
Kata._if(true, () -> System.out.println("true"), () -> System.out.println("false"));
// prints "true" to the System out.
Functional Programming
Fundamentals

Stats:

CreatedSep 13, 2014
PublishedSep 13, 2014
Warriors Trained57968
Total Skips4695
Total Code Submissions101337
Total Times Completed34190
JavaScript Completions14201
Ruby Completions1150
Python Completions11236
CoffeeScript Completions119
Haskell Completions876
Elixir Completions327
C++ Completions3459
Rust Completions1057
C# Completions1700
Lua Completions481
C Completions1124
NASM Completions76
Java Completions330
Total Stars325
% of votes with a positive feedback rating75% of 3180
Total "Very Satisfied" Votes2043
Total "Somewhat Satisfied" Votes661
Total "Not Satisfied" Votes476
Ad
Contributors
  • user578387 Avatar
  • jhoffner Avatar
  • dnolan Avatar
  • bekh6ex Avatar
  • Miista Avatar
  • bkaes Avatar
  • cris Avatar
  • jmlawrence Avatar
  • ah01 Avatar
  • Chrono79 Avatar
  • kazk Avatar
  • Voile Avatar
  • Madjosz Avatar
  • Souzooka Avatar
  • UnicornFreedom Avatar
  • FArekkusu Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • user3029010 Avatar
  • uniapi Avatar
Ad