| Bytes | Lang | Time | Link |
|---|---|---|---|
| 022 | AWK | 250826T133758Z | xrs |
| 037 | C | 230610T034902Z | Zoey |
| 002 | Japt m | 230609T145906Z | Shaggy |
| 001 | Thunno 2 | 230609T145400Z | The Thon |
| 015 | Arturo | 230219T213750Z | chunes |
| 012 | Lexurgy | 220119T053122Z | bigyihsu |
| 026 | Python 3 | 220118T211027Z | Alan Bag |
| 001 | Vyxal a | 210514T184800Z | a stone |
| 038 | Erlang | 210514T212253Z | Yuri Gin |
| 648 | Binary Lambda Calculus | 210513T031052Z | user1011 |
| 246 | MMIX | 210513T195251Z | NoLonger |
| 004 | Factor | 210507T024309Z | chunes |
| 014 | Zsh | 201221T041844Z | roblogic |
| nan | 6502 Machine Language 14 Bytes | 201221T011921Z | paulsope |
| 001 | APL Dyalog Unicode | 201220T102416Z | Kamila S |
| 031 | Tcl | 180327T223524Z | sergiol |
| 013 | FALSE | 180124T143309Z | 12Me21 |
| 025 | SmileBASIC | 180124T141244Z | 12Me21 |
| 030 | C++11 | 161114T041722Z | Karl Nap |
| 030 | PHP | 160923T195337Z | Mario |
| 008 | dimwit | 160923T183037Z | MCMaster |
| 001 | R | 160915T121022Z | rturnbul |
| 008 | Logicode | 160915T111016Z | clismiqu |
| 061 | Brainfuck | 160922T211504Z | Håv |
| 024 | Brainfuck | 160923T015715Z | KSab |
| 047 | dc | 160918T095608Z | seshouma |
| 046 | C | 160915T140007Z | cleblanc |
| 001 | Julia 1 Byte Thanks to Dennis [See Comments] | 160915T174817Z | Magic Oc |
| 916 | ><> Fish 9 16? Bytes | 160919T150344Z | L. Steer |
| 240 | Shakespeare Programming Language | 160919T114552Z | Copper |
| 011 | Racket | 160919T064007Z | rnso |
| 025 | Emacs Lisp | 160918T171528Z | Lord Yuu |
| 040 | Golang | 160918T115557Z | cia_rana |
| 002 | Pyke | 160918T111644Z | Blue |
| 015 | Java | 160917T215004Z | Olivier |
| 001 | Matlab | 160915T114449Z | flawr |
| 044 | dc | 160917T082412Z | juh |
| 015 | Racket | 160917T001625Z | Winny |
| 004 | Gogh | 160917T000350Z | Zach Gat |
| 034 | Scala | 160916T220149Z | Himself1 |
| 030 | Ruby | 160916T084033Z | Jatin Dh |
| 012 | Clojure | 160916T020456Z | user3810 |
| 231 | PHP 231 Bytes | 160916T211941Z | Nicholas |
| 007 | Swift 3 | 160916T182101Z | mklbtz |
| 044 | Swift2.2 | 160916T163551Z | Danwakee |
| 008 | sed | 160916T151353Z | Riley |
| 013 | Kotlin | 160916T132253Z | Czyzby |
| 012 | Clojure | 160916T130225Z | Carcigen |
| 019 | C# | 160916T120541Z | hstde |
| 027 | Scala | 160916T101558Z | corvus_1 |
| 009 | braingasm | 160915T172138Z | daniero |
| 020 | Stata | 160916T075711Z | f1rstgue |
| 007 | Common Lisp | 160916T071501Z | coredump |
| 024 | Python | 160915T113653Z | Steven H |
| 002 | Actually | 160916T051732Z | user4594 |
| 002 | J | 160915T205637Z | Conor O& |
| 045 | PHP | 160915T120246Z | Jör |
| 004 | Perl 6 | 160915T183556Z | Brad Gil |
| 058 | brainfuck 58 Bytes | 160915T183215Z | gtwebb |
| 009 | Perl 6 | 160915T174941Z | bb94 |
| 019 | Groovy 19 Bytes | 160915T173914Z | Magic Oc |
| 014 | Ruby | 160915T165913Z | daniero |
| 024 | Hoon | 160915T160036Z | RenderSe |
| 001 | 05AB1E | 160915T105747Z | Adnan |
| 058 | Java | 160915T133601Z | Numberkn |
| 009 | Labyrinth | 160915T141217Z | Sp3000 |
| 010 | Cheddar | 160915T141250Z | Downgoat |
| 024 | Python 2 | 160915T141025Z | Erik the |
| 051 | Java | 160915T124728Z | dwana |
| 003 | Retina | 160915T132346Z | Martin E |
| 007 | Perl | 160915T132240Z | Ton Hosp |
| 003 | Japt | 160915T132236Z | ETHprodu |
| 015 | PowerShell | 160915T131448Z | AdmBorkB |
| 002 | IBM/Lotus Notes Formula | 160915T131440Z | ElPedro |
| 001 | JAISBaL | 160915T123915Z | Socratic |
| 001 | MATL | 160915T114753Z | flawr |
| 007 | Mathematica | 160915T111058Z | Martin E |
| 007 | Brachylog | 160915T114002Z | Fatalize |
| 007 | Haskell | 160915T113333Z | lynn |
| 001 | Jelly | 160915T112707Z | lynn |
| 002 | Pyth | 160915T112251Z | Steven H |
| 004 | CJam | 160915T111229Z | Martin E |
| 015 | Javascript ES6 | 160915T105802Z | Bassdrop |
C, 37 bytes
f(a,l)int *a;{for(;l--;*a++=*a?0:1);}
a being the array (pointer) and l being the length of the array.
Readable version
f(t,i)int *t;{ // K&R to save bytes
for(;i--; // Run i times
*t++=*t?0:1); // Flip the value of array element and increment
}
Lexurgy, 12 bytes
Input is a string of as for truthy and bs for falsy.
These can be replaced with any two distinct characters, as a "proper" falsy value in Lexury would be an empty string, however since empty string is empty string, and infinite number of empty strings can be anywhere within a string, so two distinct characters are required.
a:
a=>b
b=>a
If you want numbers (16 bytes):
a:
\1=>\0
\0=>\1
Erlang, 43 38 bytes
n(Z)->lists:map(fun(X)->not X end,Z).
Many thanks to @Redwolf Programs for saving 5 bytes.
Binary Lambda Calculus, 64 bits = 8 bytes
0001011000000000010111001011111000001000001100101111011010000010
Note: This is a function and its list format is slightly different from the list format used by default in BLC’s I/O, so the TIO link doesn’t really do anything.
Explanation
If you don’t know what lambda calculus is, please read this Wikipedia page about it, since Wikipedia explains it better than I can.
My function takes a list of Church booleans as input. The list encoding that it uses is the one where a list is encoded as its right fold function. i.e. The encoding of list is foldr list (in pseudo-Haskell). An example of this kind of list is \c n. c (T (c F n)), where T and F are the Church booleans for true and false, respectively. This example represents the list [true, false].
Here is the lambda calculus (pseudo-?)code that I used to get my BLC code:
\l. l (\a b. \c n. c (a (\x y. y) (\x y. x)) (b c n)) (\c n. n)
(\ is abstraction, \a b. ... means \a. \b. ..., abstraction extends as far as it can, and application is left-associative.)
I then represented it using De Bruijin indices:
\1 (\\\\2 (4 (\\1) (\\2)) (321)) (\\1)
Note that 321 means ((3)2)1, not literally a De Bruijin index using the number 321.
Then I used @ to represent application (i.e. @mn means m(n)).
\@@1\\\\@@2@@4\\1\\2@@321\\1
(Note that I transcribed this “by hand,” so if my answer doesn’t work, then it’s probably a problem with this.)
I then used this to get the BLC code. (See here for information about how the encoding works and more information about BLC.)
The actual explanation is here
So I’m going to explain using a condensed version of the lambda calculus notation.
Note that my vocabulary, specifically “the accumulator’ and “the new element”, is probably wrong. I’m basically trying to paint the image of a for loop initializing the “accumulator” at a value and changing it each time it passes through a “new element” of the list.
\l.l(\abcn.c(a(\xy.y)(\xy.x))(bcn))(\cn.n)
\l.l( )( ) Right fold of l.
\cn.n Start the accumulator as the empty list.
\a a is the new element.
b b is the accumulator.
cn.c( Prepend...
a(\xy.y)(\xy.x) the logical negation of a
)(bcn) to the accumulator.
(This is a high-level overview and it’s probably possible to explain some of these concepts a better way.)
MMIX, 24 bytes (6 instrs)
00000000: 25010101 82ff0001 73ffff01 a2ff0001 %¢¢¢²”¡¢s””¢ɱ”¡¢
00000010: 5901fffc f8000000 Y¢”‘ẏ¡¡¡
Requires at least one element. Pass array as first argument, length as second.
invarr SUB $1,$1,1 // loop: i--
LDBU $255,$0,$1
ZSZ $255,$255,1
STBU $255,$0,$1 // a[i] = !a[i]
PBNN $1,invarr // if(i >= 0) goto loop
POP 0,0
Zsh, 14 bytes
Assumes input is given as arguments. Try it Online!
for z;<<<$[!z]
Or without bending the rules so much, this uses array I/0 (26B):
for z ($A)B+=($[!z])
<<<$B
6502 Machine Language - 14 Bytes
AE 00 01 CA B5 00 49 01 95 00 8A D0 F6 00
Array of bytes is in zero page. Length of array is in $0100.
Flipped array is in zero page.
Assembler code:
AE 00 01 - ldx $0100
CA - dex
B5 00 - lda $00, X
49 01 - eor #$01
95 00 - sta $00, X
8A - txa
D0 F6 - BNE (-10)
00 - BRK
(My first golf answer - sorry if format is off.)
FALSE, 13 bytes
[49^$0>][-.]#
Input is a list of 0 and 1 characters.
SmileBASIC, 25 bytes
DEF l l
ARYOP 1,l,1,l
END
Explanation:
DEF 'create a function
l 'named "L"
l 'with 0 outputs and 1 input named "L"
ARYOP 'array operation
1 'mode 1 (subtract)
,l 'set each element in L to...
,1 '1 minus...
,l 'the corresponding element in L
END 'end function definition
C++11, 30 bytes
As unnamed lambda:
[](auto&v){for(auto&x:v)x=!x;}
Accepts any standard container like vector<int> (but not vector<bool>) or int[] or bool[].
PHP, 30 bytes
foreach($argv[1] as$i)echo!$i;
Testing code:
$argv[1] = [true,true,false,true,false,false,true,false];
foreach($argv[1] as$i)echo!$i;
dimwit 8 bytes (non-competing)
R1,0}0,1
Replaces all 0's with 1's, and vice versa.
Takes input through a string of 0's and 1's, and outputs similarly. Hopefully that's acceptable ;)
Try it here!
R, 1 byte
!
Example:
> !c(TRUE, FALSE)
[1] FALSE TRUE
It also works with numerical input:
> !c(1, 0)
[1] FALSE TRUE
We're not restricted to one-dimensional arrays, either. Let's make a matrix, and randomly populate it with 0s and 1s:
> mat = matrix(rbinom(16, 1, .5), ncol=4)
> mat
[,1] [,2] [,3] [,4]
[1,] 0 1 1 1
[2,] 0 1 0 0
[3,] 0 0 0 0
[4,] 1 1 1 0
We can invert this just as easily:
> !mat
[,1] [,2] [,3] [,4]
[1,] TRUE FALSE FALSE FALSE
[2,] TRUE FALSE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE
[4,] FALSE FALSE FALSE TRUE
We can continue to do this for arbitrary numbers of dimensions. Here's an example on a four-dimensional array:
> bigarray = array(rbinom(32, 1, 0.5), dim=c(2,2,2,2))
> bigarray
, , 1, 1
[,1] [,2]
[1,] 0 0
[2,] 0 0
, , 2, 1
[,1] [,2]
[1,] 1 0
[2,] 0 0
, , 1, 2
[,1] [,2]
[1,] 0 1
[2,] 0 1
, , 2, 2
[,1] [,2]
[1,] 1 0
[2,] 1 1
> !bigarray
, , 1, 1
[,1] [,2]
[1,] TRUE TRUE
[2,] TRUE TRUE
, , 2, 1
[,1] [,2]
[1,] FALSE TRUE
[2,] TRUE TRUE
, , 1, 2
[,1] [,2]
[1,] TRUE FALSE
[2,] TRUE FALSE
, , 2, 2
[,1] [,2]
[1,] FALSE TRUE
[2,] FALSE FALSE
Doesn't work for characters, I'm afraid.
> !"Hello world"
Error in !"Hello world" : Invalid argument type.
Logicode, 9 8 bytes
out!binp
Simple, really.
Takes input as a binary string, as Logicode doesn't have support for lists (so [true, false] would be 10).
The out outputs the line's result.
The ! command calculates the NOT of every bit in the string, so something like !111 would be 000.
The binp is binary input.
1 byte saved thanks to @daHugLenny
Brainfuck, 72 69 61 bytes
,>++++++++[<------>-]<>+<[->-<]>[<+>-]<>++++++++[<++++++>-]<.
Closest i managed to get as BF dosent have Array support
Formatting is stupid, i will fix it when i get a pc... Somehow it wont let me post it as a snippet
Brainfuck, 24 bytes
,[>++[->++[<]>-]>-.>>>,]
The same as my answer to a similar question.
Relies on 8-bit wrapping cells, the cell size might not matter (untested) but wrapping definitely is. The main part the program is the >++[->++[<]>-]>- does some rather convoluted things to flip the last bit of the number.
A shorter solution of 19 bytes is
,[+[<[-->>]<--]<.,]
but this requires the < to noop if the data index is 0, instead of the more typical implementation of going into negative indeces.
dc, 47 bytes
?zsN0sI[z:az0<A]dsAx[lI1+ddsI;a1r-n32PlN>R]dsRx
The actual code to invert a boolean (0 or 1) is 1r-. The rest is for reading the list and printing it in the same order, which is quite hard because of the (LIFO) stack that dc uses to store the input.
Explanation:
?zsN0sI # read input, LIFO, initialize N = nr_elements and I = 0 (iterator)
[z:az0<A]dsAx # loop A: move the stack content into array 'a' (I add a[n] first,
#then a[n-1], ..., then a[1] and a[0] is not used)
[lI1+ddsI;a # loop R: increment iterator and push a[I] to stack (input order)
1r-n32P #invert value and print it with a space separator
lN>R]dsRx #repeat (R)
Run:
dc -f invert_boolean.dc <<< "1 0 1 1"
Output: there is a trailing separator (space) at the end, but I hope that's ok
0 1 0 0
Note: my answer is similar in concept to that of @Joe's, but there are differences in terms of I/O handling, stack manipulation (I use arrays) and lack of warnings. Do check his answer as well.
C, 46 Bytes recursive version
f(char*s){*s?putchar(*s&72?*s:*s^1),f(++s):0;}
C, 47 Bytes iterative version
f(char*s){for(;*s;putchar(*s&72?*s:*s^1),s++);}
Run using this main function
main(c,v)char**v;
{
f(v[1]);
}
and input like this
a.exe [1,0,1,1,0]
[0,1,0,0,1]
Julia (1 Byte - Thanks to Dennis [See Comments])
!
Map the logical not to all elements of collection y, works due to automatic vectorization (as Dennis explained). My previous answer was basically using `f(n)=map(!,n)' to map the logical not, but Julia does this on it's own. Jeez, what a cool language. Second time using it, still trying to learn here!
><> Fish - 9 (16?) Bytes
My second golf and my first in ><> Fish!
l?!;r0=nr
I'm not sure how to count bytes when flags are considered, but this solution works when running the python interpreter with the -v flag and then space-separated 1's and 0's. If this doesn't match spec, I can remove the answer.
Example
Input/Run:
python fish.py --code "l?!;r0=nr" -v 0 1 0 1 1
Output:
10100
If I'm not allowed to use flags, and instead need to take the input from the input stack (such as on https://fishlanguage.com/playground), the code becomes longer as I must read from the input stack (either 1 or 0 without any separation) until there are no more values, and then convert the char value of the input (1 becomes 49 and 0 becomes 48) to their decimal values before my comparison. I'm not sure how to count bytes for ><> but I believe this would be 16 bytes.
i:1+?!;f3*3+-0=n
Shakespeare Programming Language, 240 bytes
.
Ajax,.
Puck,.
Act I:.
Scene I:.
[Enter Ajax and Puck]
Puck:
Open your mind.Is hog as big as you?If so, let us return to scene II.You be sum of difference of zero and you and cat.Open thy heart!Let us return to scene I.
Scene II:.
[Exeunt]
Takes input as a string of \0 and \1 control characters. Outputs as a string of 0 or 1. If the input must be the same as the output, replace Open thy heart with Speak thy mind for no change in bytecount. If \0 and \1 can't be used, do the above, but also replace Open your mind with Listen to thy heart for a 5-byte penalty.
Ungolfed:
The Invertion of Veronan Arrays.
Romeo, who stores the element.
Juliet, who lectures him.
Act I: In which an array is inverted.
Scene I: A silent entrance.
[Enter Romeo and Juliet]
Scene II: In which Juliet pours out her heart to Romeo.
Juliet:
Open your mind. Is nothing better than thee? If so, let us proceed to scene III.
Thou art as good as the sum of the difference between nothing and thee and my
cat. Open your heart! Let us return to scene II.
Scene III: Finale.
[Exeunt]
This roughly translates to the following C pseudocode:
int romeo;
Scene1:
romeo = getchar();
if (0 > romeo) goto Scene2;
romeo = 0 - romeo + 1;
printf("%d", romeo);
goto Scene1;
Scene2:;
I'm using this interpreter. Sample run:
$ python splc.py invert.spl > invert.c
$ gcc invert.c -o invert.exe
$ echo -ne "\x00\x01\x00" | ./invert
101
Racket 11 bytes
(map not l)
Testing:
(define l '(#t #f #t)) ; define a list of booleans
(map not l)
Output:
'(#f #t #f)
Emacs Lisp, 25 bytes
(lambda(x)(mapcar'not x))
Not very creative.
Golang, 40 bytes
func(a[]bool){for i,b:=range a{a[i]=!b}}
usage
package main
import "fmt"
func main() {
a:=[]bool{true, true, false}
func(a []bool){for i,b:=range a{a[i]=!b}}(a)
fmt.Print(a) // => [false false true]
}
Java, 15 bytes
s->s.map(b->!b)
Note: s is a java.util.stream.Stream<Boolean> and the import is not necessary, proof below.
Testing and ungolfed
LookMaNoImports.java
class LookMaNoImports {
static Main.F f = s -> // transform a Stream<Boolean>
s.map( // by applying its map method
b -> // which in turns transforms a boolean
!b // by applying its negation.
);
}
Main.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
interface F {
Stream<Boolean> f(Stream<Boolean> s);
}
public static void main(String[] args) {
F f=LookMaNoImports.f;
test(f, new Boolean[]{true}, new Boolean[]{false});
test(f, new Boolean[]{false}, new Boolean[]{true});
test(f, new Boolean[]{true, false}, new Boolean[]{false, true});
test(f, new Boolean[]{true, true}, new Boolean[]{false, false});
}
static void test(F f, Boolean[] param, Boolean[] expected) {
List<Boolean> result = f.f(Arrays.stream(param)).collect(Collectors.toList());
if (result.equals(Arrays.asList(expected))) {
System.out.printf("%s: OK%n", Arrays.toString(param));
} else {
System.out.printf("%s: NOT OK, expected %s%n", Arrays.toString(param), Arrays.toString(expected));
}
}
}
Matlab, 4 1 byte
This should be self explanatory.
~
Matlab has the one-byte negation operator ~, if you want a function you can use @not.
dc, 44 bytes
Append this to a line of space-delimited input and echo it into dc. Output is delimited with newlines and punctuated with error messages (due to the blatant abuse of k). If you quote the stuff, it'll give you even more errors. (When we re-vamp dc in the next version, we should probably do something about that.) I suggest pairing with a 2>nul or other system-specific hack for suppressing whiny programs.
0sz[1r-SAlz1+szz0<a]dsax[lzd1-szLApk0<a]dsax
Explained:
# "ToS" := "top of stack"
0sz # Initialise register `z' with 0: this will hold the length of our input
[ # Open macro definition
1r- # Replace ToS with !(ToS): 1-1==0, 1-0==1
SA # Store this result on the top of stack `A'
lz1+sz # Increment z, our length counter
z0<a # If the stack depth is positive, repeat (we still have input to negate)
]dsax # Store a copy of macro in register `a' and execute it
# At this point, our input has been negated and stored in A,
# first-in-first-out orientation
[ # Open macro definition
lzd1-sz # Decrement z and keep a copy of it for later
LA # Push ToS(A) onto stack; pop from A
p # Peek at ToS
k # `k' is for KILL THE TOP OF STACK BWAHAHAHA
0<a # The value from register `z' is now on top: if positive, we still have
# values to print, so repeat macro
]dsax # I'm in a chaotic mood, so let's just re-use `a'
Racket, 15 bytes
(curry map not)
Basically the Haskell answer.
Gogh, 4 bytes
{!}m
Usage:
$ ./gogh -noa '{!}m' "1 0 1 0 0 0 1"
[0 1 0 1 1 1 0]
Scala, 36 35 34 bytes
def f(a:Array[Boolean])=a.map(!_)
Discovered I could remove the second space. Discovered I could remove brackets.
Ruby, 30 bytes
gets.split(?,).map{|e|!eval e}
"true,false" outputs false,true.
Clojure, 12 bytes
(map not x)
as in:
(map not [true false true])
=> (false true false)
Updating due to the objection that this is not a function or program:
#(map not %)
Returns a function that nots anything passed to it.
PHP (231 Bytes)
function flip_bool_array($array) {
$newarray = array();
foreach ($array as $bool) {
if ($bool === true) {
$bool = false;
} else {
$bool = true;
}
$newarray[] = $bool;
}
return $newarray;
}
Explanation
The function accepts an array and loops over it testing each value for a true value and setting it to false, if the value is not true then it is set to true. Each boolean is added to a new array and returned.
Swift 3 (7 bytes)
.map(!)
e.g.
[true, false].map(!)
Explanation
Seems pretty obvious. Calls map on the array [true, false]. The one "gotcha" is that, in Swift, operators are just functions and can be passed around as arguments. This means map(!) is passing the "not" function ! into map.
Swift(2.2) 44 bytes
I am using x as the input variable here. For example,
let x = [true,false]
¯\_(ツ)_/¯
Golfed
let y = {let b = $0.map({!$0});print(b);}(x)
unGolfed
let y = {
let b = $0.map({!$0})
print(b);
}(x)
sed 8
y,01,10,
Takes input as 1s and 0s.
Kotlin, 13 bytes
{it.map{!it}}
This is a lambda of (List<Boolean>)->List<Boolean> type (or (BooleanArray)->List<Boolean>, as map function on arrays returns lists instead of arrays).
{it.map{!it}.toBooleanArray()} would return the result as an actual array.
Clojure, 12 bytes
#(map not %)
Basically the same as the Haskell answer. Unfortunately, Clojure doesn't have implicit partial application; thus the function macro.
C#, 19 bytes
as an annonymous function, takes a bool[] and returns an IEnumerable
b=>b.Select(x=>!x);
or in 36 bytes with
dynamic f(bool[]b)=>b.Select(x=>!x);
Scala, 27 bytes
def f(a:Boolean*)=a map(!_)
Takes a bool array as varargs and maps each element to its inverse.
braingasm, 9 bytes
,[48-z:,]
Assumes that input from stdin is a string of only "0"s and "1"s, i.e. bytes containing the value 48 or 49. Prints the negated values to stdout.
How it works:
Read one byte from stdin (,), and loop ([]) until the end of the input. For each byte, subtract 48 (48-) and print 1 if the result is 0 and vice versa (z is the zero-flag, : prints the given integer value), then get another byte from stdin (,)
Run it like e.g. $ echo -n 001011 | braingasm invert.bg and get 110100
Stata, 20 bytes
recode x (0=1) (1=0)
x is the input, and the rest is self-explanatory
Common Lisp, 7
bit-not
Negate bits on a bit array.
Example
(bit-not #*00101010010100101001010010101)
=> #*11010101101011010110101101010
Python, 27 25 24 bytes
Thanks to Lynn for golfing off two bytes, and xnor and Mego for golfing off another.
lambda a:[b^1for b in a]
J, 2 bytes
-.
This is the negation verb.
Test case
-. 0 1 0 0 1
1 0 1 1 0
PHP , 45 Bytes
<?foreach($_GET[a]as$v)$a[]=1-$v;print_r($a);
Perl 6, 4 bytes
"French"/Unicode version:
!«*
"Texas"/ASCII version:
!<<*
Input is a single value which can be treated as a list.
This is a a Whatever lambda (*) with the logical not prefix operator (!) combined using prefix hyper operator («).
Effectively this is the same as:
-> $_ { $_.values.hyper.map: &prefix:<!> }
# ( currently the Rakudo implementation doesn't actually do the 「.hyper」 call,
# but prefix 「«」 is specifically designated for doing things in parallel )
Usage:
# pretend it's a method
say (True,False,True,True).&( !«* );
# (False True False False)
say ( !«* )( (False,False,True,False,False) );
# (True True False True True)
# give it a lexical name
my &list-invert = !«*;
# v¯¯ a space is necessary here
say list-invert (True,False);
# (False True)
say (False,True).&list-invert;
# (True False)
brainfuck (58 Bytes)
-[>+<-----]>--->,[<[->->+<<]>[--<]>[>]<[-<+<+>>]<+.[-]<>,]
Ungolfed
-[>+<-----]>--- Number 48 (stands for 0)
>, Read in first point
[
<[->->+<<] Subtract 1 from 48 flag, subtract 1 from read data, add 1 for new flag
>
[--<] If sitting on 1 (true) subtract 2 and move left)
>[>]< Move to 48 flag
[-<+<+>>] Add 48 to data point
<+.[-]< Add 1 move print, zero cell, move to new 48 cell
>, Read in next point
] Loop if input remaining
Takes an input of undivided 1s or 0s (11001).
Perl 6, 9 bytes
*.map: !*
Usage:
say (*.map: !*)((0, 1, 1)) # (True False False)
Groovy (19 Bytes)
{x->x.collect{!it}}
Simple mapping function in a closure.
Ruby, 14 bytes
Anonymous function:
->a{a.map &:!}
Test it:
->a{a.map &:!}.call([true, true, false, true, false, true, true])
# => [false, false, true, false, true, false, false]
Hoon, 24 bytes
|*
*
(turn +< |=(? !+<))
Creates a generic gate, map over the contents of the list, negate all loobeans in it.
This uses the normal Hoon tricks, namely using a generic ("wet"/|*) gate to avoid having to specify the sample type, along with having unnamed samples (* or ?) and using tree navigation syntax (+<) to fetch them.
> a
~[%.y %.n %.n %.y %.n %.y]
> =f |*
*
(turn +< |=(? !+<))
> (f a)
~[%.n %.y %.y %.n %.y %.n]
Java, 58 bytes
void f(boolean[]a){for(boolean i:a)System.out.print(!i);}
Labyrinth, 9 bytes
,$:)%#$.,
Try it online! Assumes newline-separated input with a trailing newline. Thanks to @MartinEnder for help with golfing.
This program's a bit weird for a Labyrinth program - it doesn't make use of the 2D nature of the language, and it actually bounces back and forth. On the first forward trip, we have:
[Moving rightward]
, Read char c of input
$ XOR c with implicit 0 at bottom of stack
:)% Calculate c % (c+1), erroring out if c == -1 from EOF, otherwise returns c
#$ XOR with (length of stack == 1)
. Output (c^1) as char
, Read newline
[Moving leftward]
. Output newline
$ XOR two implicit 0s, stack [0]
%# Mod with (length of stack == 1), giving stack [0]
$:) Increment, duplicate then XOR, stack still [0]
, Read char c of input
The next occurence of $ then XORs the existing 0 on the stack with c, as opposed to an implicit 0 at the bottom of the stack like in the first run. Both situations leave the stack as [c], and the program repeats thereafter.
Alternative 9-bytes:
,:):/$.:,
,::)/)$.,
,:):%)$.,
Cheddar, 10 bytes
@.map((!))
I hope I counted right as I'm writing from phone
Python 2, 24 bytes (non-competing)
lambda a:[i-1for i in a]
Logic is similar to Steven's, but I tried to use this comment's idea, but different, because it still takes 0/1 arrays, not 0/-1. There is no byte shaving for using 0/-1, so let's be sane. Note that this is non-competing, until Steven or Lynn allows me to use the idea. If so, I might remove the non-competing mark. Note that this code cannot be shamelessly stolen, it's still here. Only Steven can use it for his answer.
Java, 51 bytes
l.stream().map(b->!b).collect(Collectors.toList());
l = a Collection of Booleans I feel like this could be shorter, then again golfing in java ^^
Perl, 7 bytes
Includes +2 for -lp
Give each boolean value as 0 or 1 on its own line
invert.pl
1
1
0
^D
invert.pl:
#!/us/bin/perl -lp
$_^=1
Japt, 3 bytes
¡!X
Japt doesn't have boolean input, so input is an array of 0s and 1s. Test it online!
How it works
¡ // Map each item X in the input to
!X // the boolean NOT of X.
// Implicit output
PowerShell, 15 bytes
$args[0]|%{!$_}
I think this may even work in v1, hence I left the version number off the title. Loops through the input $args and negates each item in turn. That resulting array is left on the pipeline.
The neat thing, however, is because PowerShell is so loose on its casting requirements, you can do a completely mixed input and get an appropriate Boolean output. For example, here are the literal Boolean values $false/$true, the numbers 0 1 and 123456789 as integers, an empty string, a non-empty string, an empty array, and a non-empty array --
PS C:\Tools\Scripts\golfing> .\invert-a-boolean-array.ps1 @($false,$true,0,1,123456789,'','foo',@(),@(1,1))
True
False
True
False
False
True
False
True
False
IBM/Lotus Notes Formula, 2 bytes
!a
Usage:
Create a Notes form with two fields named a and b.
a (input) = editable, number, multi-value, comma separated
b (output) = computed, number, multi-value, comma separated
Paste the above formula into b and give a a default value of 0.
Create a new document with the form, enter a binary list in a and press F9 to update the output.
Examples:
Works because given a list as input, Notes formula will apply whatever specified action to every element in the list.
JAISBaL, 1 byte
!
Like all the other 1-byte answers, this is the negation operator, which can operate over an array if needed. This leaves the output on the stack, which is printed at the end of the program.
For two bytes, the array can be explicitly printed:
!§
Input is in JAISBaL's incredibly odd array format (which I did invent, but I don't like it...).
Test Cases (Output from the Java interpreter, 3.0.5):
Enter a value > [true][false]
--------------------
Stack: [[false, true]]
Locals: {}
----------------------------------------
Enter a value > [false][false][true][false][false]
--------------------
Stack: [[true, true, false, true, true]]
Locals: {}
MATL, 1 byte
~
~ is the logical not and as many functions, it can also be applied to arrays/matrices.
Mathematica, 7 bytes
Not/@#&
or without letters:
!#&/@#&
As for the syntactic sugar: & marks the right end of an unnamed function and has very low precedence. # refers to the first argument of the nearest and enclosing &. ! is the operator for Not. So !#& is just an unnamed function that negates its argument, in other words its identical to the built-in Not. /@ is the operator for Map. So the code would also be equivalent to the somewhat more readable Map[Not, #]&.
Brachylog, 7 bytes
:{-$_}a
Explanation
:{ }a Apply this predicate to each element of the Input
- Decrement
$_ Negate
Haskell, 7 bytes
map not
Example:
Prelude> (map not) [False, True, True]
[True,False,False]
Javascript ES6, 15 bytes
a=>a.map(b=>!b)
Not much explanation needed I guess
f=
a=>a.map(b=>!b)
a.innerHTML = `[true, false, 1, 0] => ${ f([true, false, 1, 0]) }`
<pre id=a>


