Is it a brainfuck instruction?

A very simple challenge today. Output a truthy value whether the ASCII character (or it's code) supplied is a brainfuck instruction (one of +-,.<>[]), and a falsy value if it's a comment (everything else).

Input

+
#
<
>
.
P
,


Output

true
false
true
true
true
false
true

• As in, can the answer be formatted as either a program or function? (such as requiring main(){} and all of that in C answers) Apr 11 '20 at 17:57
• @RedwolfPrograms doesn't matterdoesn't matter Apr 11 '20 at 18:02
• Is the input character guaranteed to be printable ASCII? (32-126) Apr 11 '20 at 18:47
• No, but I'd like to see (a part of an already valid answer) that assumes it's printable as an "addon". As I've stated (or rather deleted the comment afterwards), as it's quite a boring challenge, I'm looking for some creativity Apr 11 '20 at 18:50
• @LuisMendo No, according to the esolang wiki, it's often lowercase Apr 12 '20 at 15:56

brainfuck, 105103 95 bytes

Outputs \x00 for false and \x01 for true.

,>>>+++++++[<++++++<++++>>-]<+[<<->>-]<<[-[-[-[--------------[--[>+[<->-]<[--[<->[-]]]]]]]]]<+.


Try it online!

The general principle is comparing against all values (decreasing partial distances of the char-codes), using loops to logically OR the results, and if none matched - change the output cell from 1 to 0.

See my 4 answer for a more detailed explanation (4 instructions read more easily).

unsure, 1302 bytes

um um yeah err hm yeah uhhhhhhhh then ummmm uhhh errrr uhh errr ummmmm yeah err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops oops oops oops oops oops um then heh then uhhhhhhhhh no oops oops then um err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops oops oops oops oops um then heh then uhhhhhhhh no oops oops then um err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops oops oops oops um then heh then uhhhhhhh no oops oops then um err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops oops oops um then heh then uhhhhhh no oops oops then ummmmmmm uh errr uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops oops um then heh then uhhhhh no oops oops then umm err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops um then heh then uhhhh no oops oops then ummmmmm uhhh ummmmm errrrrr uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops um then heh then uhhh no oops oops then umm err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops um then heh then uhh no oops oops oops okay


This was not easy. Unsure is a new stack based language I've created, that uses words people tend to say when they're not sure about something.

It's kind of hard to explain how such a large program works, but I'll try to give a high-level explanation. First, here's an expanded version of this program:

um um yeah err hm yeah uhhhhhhhh then ummmm uhhh errrr uhh errr ummmmm yeah err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops oops oops oops oops um then heh then uhhhhhhhhh no oops oops then um err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops oops oops oops um then heh then uhhhhhhhh no oops oops then um err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops oops oops um then heh then uhhhhhhh no oops oops then um err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops oops um then heh then uhhhhhh no oops oops then ummmmmmm uh errr uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops um then heh then uhhhhh no oops oops then umm err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops um then heh then uhhhh no oops oops then ummmmmm uhhh ummmmm errrrrr uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops um then heh then uhhh no oops oops then umm err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops um then heh then uhh no oops oops

oops okay


The first part sets up the stacks. There are two stacks, one of which is selected as the active stack. The first stack contains a 0 at the bottom (the return value), as well as 8 copies of the input character's ASCII character code, negated.

After this, there are 8 sections to check for equality (first one shown as an example):

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops oops oops oops oops um then heh then uhhhhhhhhh no oops oops then um err uh


The first line will pop the first stack and add it to the first, which in this case is 43 (the character code for +). It also pushes 1 to the first stack. The next line is essentially an if statement, which is used to create a NOT operator by pushing 0 to the first stack if the result of adding the character code is not 0 (meaning they are not the same).

The third line is the most complicated. It starts with a sort of else statement, as it only runs when the second line's if statement doesn't push 0 (and therefore the character code was 43). It will then replace the bottom of the first stack with 1, the new output.

Finally, after the "else statement", the character code is incremented to 44, for ,, and the process is repeated 7 times.

The last line, oops okay, just outputs the bottom item in the first stack, as the previous operations have cleared it out except for one copy of the input (oops discards the top of the stack, and okay outputs).

There's definitely a few bytes I could golf here and there, might work on it later.

• um err uh okay uh well then um uh yeah um sure Apr 23 '20 at 20:57
• Why is like not in there? Jul 13 '21 at 9:18
• @EnderShadow8 Not sure how to answer (luckily this language gives me lots of options to express that :p) Jul 13 '21 at 14:06

Python, 16 bytes

'+-,.<>[]'.count


Try it online!

An anonymous method is shorter here than a lambda. See my tip Object method as answer.

• <built-in method count of str object at 0x101520730>? Apr 12 '20 at 15:32
• The str object "at 0x101520730" is the string literal in the program. It has a built-in method named count. This method will return 1 only if given an instruction character. You can call it in the normal way by providing an immediate parameter e.g. ('.') or you can assign it to a variable (e.g. f) and call f later.
– Neil
Apr 13 '20 at 13:53
• @VictorVosMottorthanksMonica Note also that the usual submissions of lamdba's are represented as a similarly weird value like <function <lambda> at 0x7fd4f39d7758>.
– xnor
Apr 13 '20 at 21:37
• Anything that doesn't have a __str__ or a __repr__ has that. Apr 14 '20 at 2:04

Retina 0.8.2, 9 bytes

[]+-.<>[]


Try it online! Link includes test cases. Explanation: This is actually a character class; the outer []s delimit the class, as (by being placed immediately after the opening [) the first ] is actually part of the class. The , isn't needed as the - creates a range from + to . which helpfully includes the - itself as well as the ,.

• I am now worried that the stuff that parses regular expressions is actually a backtracking regex engine... Apr 12 '20 at 3:37

vJASS (Warcraft 3), 299278 265 bytes

Using //! import zinc "<code_path>" command to exclude //! zinc and //! endzinc.

Prints T (True) and F (False).

library Q{trigger W=CreateTrigger();integer E;function onInit(){TriggerRegisterPlayerChatEvent(W,Player(0),"",false);TriggerAddAction(W,function(){for(0<=E<8){if(GetEventPlayerChatString()==SubString("+-<>.,[]",E,E+1)){BJDebugMsg("T");return;}}BJDebugMsg("F");});}}


library CodeGolf{
trigger chatEvent = CreateTrigger();
integer Index;

function onInit(){
TriggerRegisterPlayerChatEvent(chatEvent, Player(0), "", false);
for (0 <= Index < 8){
if(GetEventPlayerChatString() == SubString("+-<>.,[]", Index, Index + 1)) {
BJDebugMsg("T");
return;
}
}
BJDebugMsg("F");
});
}
}


Output:

05AB1E, 11 bytes

+1 due to a bug fix noted by @Kevin Cruijssen

-1 thanks to @Kevin Cruijssen

"+-,.<["ºIå


Try it online!

an interesting approach, but sadly it's longer and it doesn't work:

•q”;ιÚ•2ôIÇ;îå


Try it online!

• Your 11-byter should be "+-,.<>[]"Iå or "+-,.<>[]"så. You want to check if the input is in that string, not if that string is in the input. And you can save a byte by using the mirror builtin: "+-,.<["ºIå Apr 14 '20 at 7:59

4, 131 bytes

Outputs \x00 for false, \x01 for true.

3.799600006010160202614146292964343199994389919999018991999901899199990189919999148991999902899199992989919999028995004999999995014


Try it online!

Explanation

4 doesn't have a simple comparison or condition instructions, so I used while-not-zero loops to fake them and match by distances:

3.

6 00 00            # set constants
6 01 01
6 02 02
6 14 14
6 29 29
6 43 43

7 99               # input as integer

1 99 99 43 - 8 99  # for each brainfuck character
1 99 99 01 - 8 99  # compare to the input,
1 99 99 01 - 8 99  #  (subtracting differences to the input)
1 99 99 01 - 8 99  #  and jump to the end if true,
1 99 99 14 - 8 99  #  otherwise start a loop (to fake branching)
1 99 99 02 - 8 99
1 99 99 29 - 8 99
1 99 99 02 - 8 99
5 00 4             # if all comparisons failed, print 'false' and exit
9 9 9 9 9 9 9 9    # close loops

5 01 4             # print 'true' and exit


(elem"+-.,<>[]")


Try it online!

Low-effort unnamed function.

• For convenience with pointfree TIO submissions, you can add the -cpp flag and use backslash to continue a line like this: Try it online!
– xnor
Apr 13 '20 at 21:39
• @xnor ah I tried to do that without the flag, ty Apr 13 '20 at 23:30

Python 3, 26, 23 bytes

lambda s:s in'+-,.<>[]'


Try it online!

• Is this really a valid format? Apr 12 '20 at 6:55
• @dingledooper , I might have gotten mixed up. I'm pretty new, still don't know all the valid formats. The only reason I changed this from my old answer was because I saw xnor's answer, which I thought was invalid, but, hey - he's way more experienced than me, so I probably just didn't know something. Am changing back to my old one.
– Dion
Apr 12 '20 at 6:59
• For your information, xnor's answer is still a function, in that it can be called with a parameter. Your previous answer was not, it was just a snippet since in is a keyword that doesn't make a function
– Jo King
Apr 12 '20 at 12:32
– xnor
Apr 13 '20 at 21:59
• This isn't a valid answer; it's an expression, not a function. May 30 '20 at 15:38

Keg, -rt, 10 bytes

-[]+-,.<>


This:

• Pushes the string []+-,.<> onto the stack
• Subtracts that string from the input.

If it is a BF instruction, an empty string will be printed, otherwise, the input character will be printed.

Python 3, 26 bytes

print(input()in"+-,.<>[]")

• You need something to output the result, usually print. See the Python I/O summary.
– xnor
Apr 13 '20 at 21:40
• I'm pretty sure you can use exit instead of print. Apr 24 '20 at 6:14

C (gcc), 28 bytes

f(c){c=index("+-,.<>[]",c);}


No math here, no math there, no math anywhere.

Try it online!

Pyke, 3 bytes

Built-ins FTW!

~B{


Try it online!

Explanation

~B  "><+-.,[]"
{ Does it contain input?


Desmos, 43 bytes

1\left\{42<i<47,i=60,i=62,i=91,i=93\right\}


Try it online

Desmos doesn't support strings, so this turns from relatively trivial string comparison to relatively trivial integer comparison. I tried to compress the higher numbers, but I couldn't find a nice way to exclude 61 and 92, and the sequence doesn't show up in OEIS.

• Where exactly can we test desmos answers at? Please do provide a link in later submissions. Aug 24 '20 at 2:21
• @Razetime You can test Desmos answers at the link provided at Desmos (note: I accidentally put the wrong one for this question, fixing that as well). I didn't bother to generate a specific link for this answer as it's trivial, but I probably should have. Adding it now. Aug 24 '20 at 3:49

SNOBOL4 (CSNOBOL4), 45 bytes

 "<>[]+-,." INPUT :F(F)
X =1
F OUTPUT =X
END


Try it online!

1 for truthy, empty line for falsey.

JavaScript (Node.js), 25 23 bytes

-2 thanks to @Arnauld

c=>/[+-.<>[\]]/.test(c)


Try it online!

• 23 bytes Apr 11 '20 at 20:01
• @Arnauld Forgot about .test, thanks! Apr 11 '20 at 20:16
• You can just use /[+-.<>[\]]/.test - point-free Aug 24 '20 at 7:59
• @pxeger Doesn't work. Try f=/[+-.<>[\]]/.test then f("[") Aug 24 '20 at 13:14
• @RedwolfPrograms wow I should never under-estimate the stupidity of JavaScript Aug 24 '20 at 15:29

Pyth, 10 bytes

/"+-,.<>[]


Try it online!

Kotlin, 18 bytes

{it in "+-<>[].,"}


Try it online!

Arn, 9 bytes

[‚‚◄┴´£Q7


Try it!

Explained

Unpacked: "+-.,<>[]"&

  "+-.,<>[]" Literal string
& Contains element
_ Variable initialized to STDIN; implied


Scala 2.12, 15 bytes

"<>+-.,[]"toSet


This takes the string of all brainf*** instructions and turns it into a set. Since Scala's sets are also predicates, we can treat it like a function that returns true when it's a valid instruction.

Scala 2.12 is required for postfix operator toSet.

Try it online

Jelly, 10 bytes

ẇ“+-<>[],.


Try it online!

How it works

ẇ“+-<>[],. - Main link. Takes a character C on the left
“+-<>[],. - Yield "+-<>[],."
ẇ          - Is C in that string?


Red, 27 bytes

""= exclude input"+-,.<>[]"

• Welcome to the site, and nice first answer! Would you mind editing in a link to an online interpreter such as TryitOnline, so that other's can test and verify your code? Nov 2 '20 at 0:37

Julia 1.0, 14 bytes

in("+-,.<>[]")


Try it online!

some Julia functions will return an anonymous function when fed with not enough arguments, basically in(x) = y -> in(y,x). It would be a bit more interesting with ∈ but it costs 1 more byte.

Pxem (pxem.mktemp.emb.posixism, BREAK_LOOP_WHEN_INSUFFICIENT=1), filename only: 33 bytes.

The \0001 is an unprintable whose codepoint is 0x01.

[<>+-,.].i.t@.w.m.-\0001.yT.o.d.a.c.a


Try it online!

Accepts one character from stdin as an input. Outputs a T for truthy, nothing for falsey.

Pxem (pxem.mktemp.emb.posixism, BREAK_LOOP_WHEN_INSUFFICIENT=0), filename only: 42 bytes.

The \0001 is an unprintable whose codepoint is 0x01.

[<>+-,.].i.t@.w.c.c.z.d.a.m.-\0001.yT.o.d.a@.a


Try it online!

Accepts one character from stdin as an input. Outputs a T for truthy, nothing for falsey.

Factor, 17 bytes

[ "+-,.<>[]"in? ]


Try it online!

Ly, 14 bytes

"+-,.()[]"i~u>


Try it online!

"+-,.()[]"      - Push legit characters onto the stack
i     - Read codepoint from STDIN
~    - Pop stack, search stack for that codepoint, push 1|0 result
u   - Print top of stack as a number (will be 1 or 0)
>  - Switch stacks to avoid printing the current contents


jq, 18 bytes

inside("+-,.<>[]")


Try it online!

• Fix+4 thanks to @JoKing
• Bounty started. By the way, this is 100 because SE has silly rules saying that if you've posted an answer, the minimum bounty is +100. Sep 2 '21 at 8:56
• The output is always true for every input. Sep 3 '21 at 0:07
• You should be using inside
– Jo King
Sep 4 '21 at 7:54
• I'll award the bounty once this answer becomes valid. Sep 8 '21 at 19:49
• @emanresuA fixed thx to jo king, if you have already lost 100 rep for prev. Bounty you are free to not award the bounty again Sep 9 '21 at 7:41

C (gcc) XPG4.2/Single UNIX Specification, Version 3, POSIX:2001, 30 bytes

f(c){c=!!index("+-,.<>[]",c);}


Try it online!

• nitpick: index() isn't included by the C standard, so shouldn't it be stated? Apr 11 '20 at 18:17
• @FryAmTheEggman Ah, well deciphered! Ok, posted the version of POSIX it's good to go on. Thanks! :-) Apr 11 '20 at 18:40
• This is totally not necessary. C (gcc) works just fine. Apr 23 '20 at 20:44

C (gcc), 31 bytes

f(c){c=!!strchr("+-,.<>[]",c);}


Try it online!

Ruby-nl, 16 bytes

Input is STDIN. Prints 0 (Ruby truthy) or nil (falsy).

Annoyingly, it seems like Ruby demands that both [ and ] be escaped in the character class regex.

p~/[+-,.<>]/
`

Try it online!

• That's apparently because Ruby allows nested character classes.
– Neil
Apr 12 '20 at 9:31