7
\$\begingroup\$

Challenge is to write a function/program which outputs a truthy or falsey value alternately each time you call it. No input.

It can start with either true or false.

\$\endgroup\$

closed as unclear what you're asking by Luis Mendo, AdmBorkBork, Dada, Sriotchilism O'Zaic, NoOneIsHere Mar 17 '17 at 16:45

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 10
    \$\begingroup\$ I would recommend posting your challenges to the sandbox first for feedback next time. \$\endgroup\$ – fəˈnɛtɪk Mar 17 '17 at 14:11
  • 1
    \$\begingroup\$ Allowing programs may be problematic. What can be assumed between program executions? Can it be assumed that any variables created by the program will be seen by the next execution? More generally, to which extent is "state" preserve between executions? \$\endgroup\$ – Luis Mendo Mar 17 '17 at 14:49
  • 1
    \$\begingroup\$ I think you're starting to give people too much liberty. Nothing between calls is a bit iffy for tacit languages (including stack-based ones), as you can operate on the previous return value for free. If full programs are valid or not is also unclear at this point. You should clarify these things in the question. \$\endgroup\$ – Dennis Mar 17 '17 at 16:47
  • 1
    \$\begingroup\$ Most answers here rely on externals or globals which could change between calls. \$\endgroup\$ – G B Mar 17 '17 at 19:26
  • 1
    \$\begingroup\$ Curiously, on the day this challenge was posted, Mathematica released, in v. 11.1, the function, PersistentValue, which supports values that persist between sessions. (reference.wolfram.com/language/ref/PersistentValue.html) \$\endgroup\$ – DavidC Mar 18 '17 at 14:49

18 Answers 18

8
\$\begingroup\$

Jelly, 3 bytes

®¬©

Try it online!

How it works

®¬©  Main link. No arguments.

®    Retrieve the value stored in the register.
 ¬   Negate it.
  ©  Copy the result to the register.
\$\endgroup\$
  • 6
    \$\begingroup\$ This looks like a distraught frog and I can't stop laughing about it. \$\endgroup\$ – Quentin Mar 17 '17 at 15:55
  • \$\begingroup\$ According to this comment, ¬ might be valid as well. Dibs on the 1-byte solution in case this turns out to be allowed. \$\endgroup\$ – Dennis Mar 17 '17 at 16:49
4
\$\begingroup\$

Python 2, 30 bytes

def f():f.n^=1;print f.n
f.n=0

Try it online!

\$\endgroup\$
4
\$\begingroup\$

JavaScript ES6, 11 bytes

f=i=_=>i=!i

f=i=_=>i=!i

console.log(f());
console.log(f());
console.log(f());

\$\endgroup\$
  • \$\begingroup\$ This doesn't work. If I run it again with a clean environment, it fails. \$\endgroup\$ – Rɪᴋᴇʀ Mar 17 '17 at 14:37
  • \$\begingroup\$ Uh... wow, ok, that works. That's really clever, actually :D \$\endgroup\$ – ETHproductions Mar 17 '17 at 14:49
4
\$\begingroup\$

Python 2, 34 33 32 bytes

-1 byte thanks to Cyoce

def f(x=[]):x+=0,;print len(x)%2

Try it online!

This works because the list used as (optional) argument is kept between function calls.
It's not a bug, it's a feature

\$\endgroup\$
  • \$\begingroup\$ Does def f(x=[0]):x[0]^=1;print x[0] work? \$\endgroup\$ – Neil Mar 17 '17 at 19:39
  • \$\begingroup\$ @Neil yep, it works \$\endgroup\$ – Rod Mar 17 '17 at 19:58
3
\$\begingroup\$

Java 7, 36 35 bytes

int f=0;boolean c(){return++f%2<1;}
// OR
boolean f;boolean c(){return f=!f;}     // Thanks to @Poke

Old answer: int f=0;boolean c(){return(f^=1)<1;}

Extremely short for a Java 7 answer. :D (Although still the longest of all submissions of course)

Explanation:

int f=0;              // Integer flag on class level
boolean c(){          // Method with no parameters and a boolean return-type
  return ++f % 2 < 1  //  return whether `f` modulo 2 is 0 (but increase `f` first)
}                     // End of method

Test code:

Try it here.

class M{
  int f=0;boolean c(){return++f%2<1;}

  public static void main(String[] a){
    M m = new M();
    System.out.println(m.c());
    System.out.println(m.c());
    System.out.println(m.c());
    System.out.println(m.c());
    System.out.println(m.c());
  }
}

Output:

false
true
false
true
false
\$\endgroup\$
  • 1
    \$\begingroup\$ boolean f;boolean c(){return f=!f;} is also 35 bytes \$\endgroup\$ – Poke Mar 17 '17 at 14:40
  • \$\begingroup\$ @Poke Oops, I've been overthinking it apparently.. \$\endgroup\$ – Kevin Cruijssen Mar 17 '17 at 14:43
  • \$\begingroup\$ Your answer works nicely too :] The bytecount of boolean is enough to dissuade me in a lot of cases, haha \$\endgroup\$ – Poke Mar 17 '17 at 14:51
  • \$\begingroup\$ @Poke Exactly, that's why I had rejected boolean from a possible solution right away for this challenge. ;) \$\endgroup\$ – Kevin Cruijssen Mar 17 '17 at 16:02
2
\$\begingroup\$

CJam, 6 bytes

{T!:T}

Try it online!

Pushes T (initially 0, falsy), negates it (toggling between 1, truthy, and 0), stores the result in T for the next invocation and leaves it on the stack.

\$\endgroup\$
2
\$\begingroup\$

Octave, 4 bytes

i=~i

i is initially sqrt(-1). Everything that's not 0 is considered true in Octave (note, this doesn't work in MATLAB).

i=~i assigns not(true) to i, making it false, The next iteration it will be false again.

This is a full program, that can be called using the file name. Example from octave-online with a file name called inverter.m:

octave:1> source("inverter.m")
i = 0
octave:1> source("inverter.m")
i =  1
octave:1> source("inverter.m")
i = 0
octave:1> source("inverter.m")
i =  1
\$\endgroup\$
2
\$\begingroup\$

Python 3, 31 bytes

def f(x=[]):x+=print(len(x)&1),

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 12 bytes

Either of these works:

(x=x=!=1>0)&
(x=x===0>1)&

I would love to express the True or False at the end using only x and = or maybe ! as well, but unfortunately, that gets longer.

In the notebook environment we can also use this 8-byte solution:

%=!=1>0&

But it only works if the call to the function is the only thing on its line and there are no other function calls in between, so I won't count this one.

\$\endgroup\$
1
\$\begingroup\$

C, 39 30 26 19 18 Bytes

Using a Macro now

i;
#define f i++%2

Thanks for the help i=!i

i;f(){return i=!i;}

i;f(){return i++?i=0,1:0;}

i;f(){putchar(i++?i=0,70:84);}

f(){static i=0;putchar(i++?i=0,70:84);}

main()
{
    putchar(48+f);puts("");
    putchar(48+f);puts("");
    putchar(48+f);puts("");
    putchar(48+f);puts("");
}

Output

0
1
0
1
\$\endgroup\$
  • 1
    \$\begingroup\$ You could change the return to i=!i Try it Online! \$\endgroup\$ – Riley Mar 17 '17 at 14:16
  • 1
    \$\begingroup\$ i;f(){return++i%2;} is shorter [19 bytes] (based on my Java 7 answer). Try it here. \$\endgroup\$ – Kevin Cruijssen Mar 17 '17 at 14:36
1
\$\begingroup\$

Bash, 11 bytes

f()((n^=1))

Output is via the function's status code.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Ruby, 15 14 12 bytes

->{0>$.^=-1}
\$\endgroup\$
1
\$\begingroup\$

PHP, 31 25 bytes

Thanks to user63956 for golfing 6 bytes

function(){echo$_GET^=1;}

Try it online!

This is an anonymous function.

\$\endgroup\$
  • \$\begingroup\$ You can use superglobal variables: function(){echo$_GET^=1;}. \$\endgroup\$ – user63956 Mar 18 '17 at 1:30
  • \$\begingroup\$ @user63956 Weird, I tried it with $argv and $argc and it didn't work so I didn't even try $_GET. Thanks. \$\endgroup\$ – Business Cat Mar 18 '17 at 4:14
0
\$\begingroup\$

Bash, 25 bytes

sed -i y/TF/FT/ $0
echo T

Try it online!

This is a full program. You must have write access to the file in addition to execute.

\$\endgroup\$
0
\$\begingroup\$

Applescript, 25

property i:0
set i to 1-i

Applescript properties are persistent. The result of the last assignment is implicitly given as the program result.

\$\endgroup\$
0
\$\begingroup\$

Haskell, 52 bytes

import Control.Monad.State
execState(modify not)True

This uses the State monad to store a boolean value which is initially set to True. Additional calls must also run within the State monad, e.g.

 execState(modify not>>modify not>>modify not)True

which evaluates to False.

\$\endgroup\$
0
\$\begingroup\$

Scala, 25 bytes

var b=8<9
def f={b= !b;b}

Explanation:

var b = 8 < 9 // define a variable b as false
def f = {     // define a function f wothout parameters
  b = !b      // invert the state of b
  b           // return b
}

There needs to be a space in b= !b, so scala doesn't want to invoke the method =! on b, which doesn't exists.

\$\endgroup\$
0
\$\begingroup\$

Java, 32 bytes

Not competing because ints aren't truth and false values in Java and an answer like that already exists.

int a;int f(){return a=a>0?0:1;}

Ungolfed(barely anything to ungolf):

int a;
int f() {
    return a = a > 0 ? 0 : 1;
}
\$\endgroup\$
  • 4
    \$\begingroup\$ Unfortunately, returning 0 or 1 isn't considered a truthy/falsey value in Java. Also, if it would have been, this would be shorter: int a;int f(){return++f%2;} (27 bytes) \$\endgroup\$ – Kevin Cruijssen Mar 17 '17 at 14:48
  • \$\begingroup\$ Instead of making it non-completing, can you edit to output truthy and falsy values? \$\endgroup\$ – Rɪᴋᴇʀ Mar 17 '17 at 20:31
  • \$\begingroup\$ @Riker An answer like this already exists \$\endgroup\$ – cookie Mar 17 '17 at 20:36
  • \$\begingroup\$ You're allowed to post an identical answer. \$\endgroup\$ – Rɪᴋᴇʀ Mar 17 '17 at 20:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.