Executing Swift code embedded into one line terminal command - xcode

I know there is the Swift REPL and the Xcode playgrounds, but I wonder whether there is an alternative to ruby -e "<code>" or sh -c "<code>" in Swift where the given one line code would be executed as the result of the command?

There isn't a direct equivalent (you can ask the swift command for its options with swift --help and see there's nothing like Ruby's -e).
But there's a workaround.
You can pass a Swift expression to the compiler direclty using echo and | (the "pipe") like this:
echo "print(42)" | swift
Result:
Welcome to Apple Swift version 2.1.1 ("700.1.101.9 700.1.78"). Type :help for assistance.
42
I guess it's similar to the behavior you were looking for.
We notice that it always prints the introduction sentence, but there's a way to fix this, by adding - at the end of the command, like this:
echo "print(42)" | swift -
Result:
42
When using literal strings, escape the double quotes:
echo "print(\"hello\")" | swift -
Result:
hello
You can execute any expression, even loops:
echo "for num in 1...5 { print(num) }" | swift -
Result:
1
2
3
4
5
etc.
It's still the REPL so it will give feedback about variables (omitting the - trick at the end), for example:
echo "let x = 42;print(x)" | swift
Result:
Welcome to Apple Swift version 2.1.1 ("700.1.101.9 700.1.78"). Type :help for assistance.
42
x: Int = 42

Related

I have a question about the "sed" statement in bash (macOS)

I'm just starting to play around with (bash/zsh) shell scripting in macOS. I'm experiencing an issue with the "sed" statement:
I have set the following variable
var1=MACBOOKPRO
when executing the following terminal command:
echo $var1 | sed -- 's/IMAC/IM/g; s/IMACPRO/IMP/g; s/MACMINI/MM/g; s/MACMINIPRO/MMP/g; s/MACBOOK/MB/g; s/MACBOOKAIR/MBA/g; s/MACBOOKPRO/MBP/g; s/MACPRO/MP/g; s/POWERMAC/PM/g'
The following output is produced: MBPRO (which is not what I want)
But when using the following command:
echo $var1 | sed -- 's/MACBOOKPRO/MBP/g; s/IMAC/IM/g; s/IMACPRO/IMP/g; s/MACMINI/MM/g; s/MACMINIPRO/MMP/g; s/MACPRO/MP/g; s/MACBOOKAIR/MBA/g; s/MACBOOK/MB/g; s/POWERMAC/PM/g'
I get a different (but correct output)! - MBP
Has anyone have a clue for me why this is happening?
Thanks in advance for your reply
Charly
Because it performs the replacements in the order you specify them. Your variable matches both /MACBOOK/ and /MACBOOKPRO/. When the first pattern is earlier in the list, it replaces the MACBOOK prefix with MB.
When you have overlapping patterns like this, you should put the longer ones first.
Another option is to change your patterns so they're anchored to match the entire string, using ^ and $:
echo $var1 | sed -- 's/^IMAC$/IM/g; s/^IMACPRO$/IMP/g; s/^MACMINI$/MM/g; s/^MACMINIPRO$/MMP/g; s/^MACBOOK$/MB/g; s/^MACBOOKAIR$/MBA/g; s/^MACBOOKPRO$/MBP/g; s/^MACPRO$/MP/g; s/^POWERMAC$/PM/g'
s/MACBOOK/MB/g; turns MACBOOKPRO into MBPRO.

Perl6 REPL usage

Is it possible to have (Rakudo) Perl6 execute some code before dropping you into the REPL? Like python does with "python -i ".
For instance, I want to load up some modules and maybe read a side file and build some data structures from that side file before dropping into the REPL and letting the user do the things they need to do on the data structure, using the REPL as a user interface.
This is similar but different than Start REPL with definitions loaded from file though answers to this question might satisfy that one. The basic case is that, at the end of execution of any program, instead of exiting, the interpreter leaves the user at the REPL. Aside from providing a nifty, built-in, Perl6-based user interface for interactive programs, it also provides a good tool from which to debug code that otherwise exits with an error.
edit:
Selecting Zoffix's solution as the correct (so far) one as it is the only one that satisfies all requirements as stated. Here's hoping this capability gets added to the compiler or language spec.
You can load modules with the -M switch.
$ perl6 -MJSON::Tiny
To exit type 'exit' or '^D'
> to-json Array.new: 1,2,3.Str
[ 1, 2, "3" ]
>
If you want to run other code, currently you have to put it into a module first.
$ mkdir lib
$ echo 'our $bar = 42' > lib/foo.pm6
$ perl6 -Ilib -Mfoo
To exit type 'exit' or '^D'
> $bar
42
>
I'd like to provide an answer that Zoffix gave on IRC. It satisfies the basic requirement but is far from pretty and it uses NQP for which there is no user support nor is the NQP API ( "nqp::*" calls ) guaranteed for the future and can change without warning.
replify 「
say 'Hello to your custom REPL! Type `say $a` to print the secret variable';
my $a = "The value is {rand}";
」;
sub replify (Str:D \pre-code = '') {
use nqp;
my %adverbs; # command line args like --MFoo
my \r := REPL.new: nqp::getcomp('perl6'), %adverbs;
my \enc := %adverbs<encoding>:v.Str;
enc && enc ne 'fixed_8' && $*IN.set-encoding: enc;
my $*CTXSAVE := r;
my $*MAIN_CTX;
pre-code and r.repl-eval: pre-code, $, :outer_ctx(nqp::getattr(r, REPL, '$!save_ctx')),
|%adverbs;
$*MAIN_CTX and nqp::bindattr(r, REPL, '$!save_ctx', $*MAIN_CTX);
r.repl-loop: :interactive, |%adverbs;
}

How to pass arguments to a golfscript program from command line

I'd like to know how to pass arguments to a golfscript program from the command line. I'm using ruby interpreter that I downloaded from golfscript.com. From what I understood from googling a bit, in ruby you pass arguments like this ruby ./program.rb 4, but doing ruby ./golfscript.rb prnt.gs 4 doesn't work (prints an empty string).
In golfscript the arguments are supposed to be made into a string and pushed to the stack.
prnt.gs's code is just p (it's supposed to print the top of the stack - the argument).
What am I doing wrong?
try running it like this:
>echo 4 | ruby golfscript.rb prnt.gs

Evaluating a mathematical expression stored as a string, into a single number (bash)

I am working on Mac OSX and using bash as my shell. I currently have a string which I wish want evaluated as a number. When I echo the string I get 1.e8*1.07**100. Is there any way to pass this string on to be evaluated as a number?
The background as to why it is a string to start with is because the expression was built step by step. First 1.e8*1.07**%%d is within the code, then the user inputs an integer to be taken as what 1.07 will be raised to the power of. So in the example above, the user would have input 100, and thus the script is stuck with 1.e8*1.07**100, which is the correct expression I was hoping for, but I would have liked it to be evaluated when I echo the variable where it is store.
Actual important bits of code:
BASE=$(printf '1.e8*1.07**%%d')
#Get user input assigned to pow
NUM=$(printf ${BASE} ${pow})
echo $NUM #1.e8*1.07**100
Thanks for any help you can offer.
[Edit: I would also like to not just echo the answer, but store it as a variable.]
How about:
python -c "print $NUM"
By the way, you could just write
BASE="1.e8*1.07**%d"
(In fact, you don't even need the quotes.)
In most unix* systems you'll find a tool called bc that can perform calculations. You'll might need to rewrite your input though, I thinks it accepts ^ instead of **, and I'm not sure about the 1.e8 notation.
It happens that perl can evaluate that exact expression
$ x="1.e8*1.07**100"
$ y=$(perl -E "say $x")
$ echo $y
86771632556.6417

Kornshell variable definition: What is ?FOO?

I found a line of code in a kornshell script:
foo=`basename ?BAR?`
What does the question marks mean?
Thank you
touch BAR ABAR ABARZ
ls ?BAR?
ABARZ
? is normally a shell wildcard char that matches 1 character, and that 1 character position must be in use, as shown in the example above. It's like a 1-char version of '*', match 1 char (that must be there). Notice that if you change to
ls ?BAR*
You get output like
ABAR ABARZ
Your code shows the same behavior
foo=$(basename ?BAR?)
echo $foo
ABARZ
Does that make sense? Not really, but given the small context you have given the other possible interpretation is that the original script writer is using ?BAR? as a place-holder and telling you "change this to a real/meaningful value".
Other may have other ideas.
IHTH

Resources