Swift

Can a programming language get more elegant than this?

Post what your favorite language looks like while solving the exact same problem.

Attached: based.png (1934x438, 87K)

Other urls found in this thread:

pastebin.com/puNkahBD
rust.godbolt.org/z/kzivqR
rust.godbolt.org/z/RYdv0I
rust.godbolt.org/z/4zKCOG
rust.godbolt.org/z/oRAXiQ
rust.godbolt.org/z/GpFqAL
godbolt.org/z/c_aonq
rust.godbolt.org/z/BDUUix
godbolt.org/z/NRf0pF
rust.godbolt.org/z/109_6d
twitter.com/SFWRedditGifs

>static
>arrow operator
not elegant

>arrow operator
>not elegant
It's cute!

Attached: IMG_2892.jpg (728x518, 123K)

Are you fucking retarded?
num.to_string().into_bytes().filter(|c| c == b'7').count()

What's the point of the "of" and "in" keywords?

pic related. They're used when you call the method.

Attached: lad.png (1150x76, 21K)

it's UGLY

I guarantee you that will perform worse.

what if the string is optional (like OP's)?

And what's the point of having different names for the same variable?

countOccurences :: Char -> Maybe [Char] -> Int
countOccurences _ Nothing = 0
countOccurences c (Just str) = length $ filter (== c) str

countOccurences 'f' $ Just "Swift is for faggots"
=> 3

Jai will probably be more elegant.

>Find a specific character from a string that can also not exist
Fuck OP is even dumber than I thought.

Rust is faster than Swift

It looks better. That's an opitional feature btw

Jai has shitty C++ punctuation soup syntax, it's not elegant

>Looks
To be expected from Apple tbqh. Maybe I'll try it, idk. Is there any graphics engine to make 2d games like Pokemon?

Go brainlet reporting in

func count(str string, char rune) uint {
var result uint
for _, c := range str {
if c == char {
result++
}
}
return result
}

> Is there any graphics engine to make 2d games like Pokemon?
Yeah, there's SpriteKit, it's built in Xcode.

> string not optional
You failed.

Jai is a perfect example of how fashion dictates programming language popularity. The language isn't even released yet and people are mentioning it as the next great thing. If it wasn't for Jonathan Blow's successful indie games nobody would bat an eye.

>Is there any graphics engine to make 2d games like Pokemon?
literally hundreds

>> string not optional
So what do you return if the string null? 0?
How stupid.

Looks like a shitty copy of Kotlin.

>20 minutes later
>no response
OP BTFO

python
import collections

def numberOfOccurences(of, in_):
return collections.Counter(in_)[str(of)]


>not using an optimized library to solve the problem

Looks cute but annoying to type I'd imagine

string.split(char).length - 1

Interesting, can you include file I/O into this. I need to benchmark it against standard go.

>static is not elegant
t. zoomer tranny ruby "developer"

While true, it's likely also the fact that Jai is the only current try we might get at full, compile time only meta programming.

Look, I made it even terser!
import Data.Maybe

countOccurences :: Char -> Maybe [Char] -> Int
countOccurences c str = fromMaybe 0 $ str >>= Just . length . filter (== c)
I don't know very much Haskell but this is fun.

>Can a programming language get more elegant than this?
Probably easily. A language that fixed up and compromised to Applels whims can't be that good.

It's was one of the features in Objective-C that many Apple developers loved, and the implementation in Swift fixes the edge cases that existed in Obj-C.

are you a qt trap?

#include
#include
#include


int numberOfOccurences(char character, std::optional string) {
if(!string) return 0;
int i = 0;
for(char c : *string) {
if(c == character) i++;
}
return i;
}

int main() {
std::cout

compile with -std=c++17

>can't detect wchars
nicely done, retard

Swift supports a much nicer FP style for problems like this.

Attached: fs.png (984x358, 22K)

It isn't a String, it's a String?

Exercise left for the reader.

You can't count the characters in an empty string so your function should reflect that, if you ask me.

It is however fairly easy to just chuck a null coalescing operator in and return 0, but that feels wrong. There aren't 0 "f"s in a nil string, there's nothing at all, but there are 0 in an empty string.

Gotta make distinction for a safe program, if you ask me.

I see where you're coming from, but that's not what OP asked.

Could you show me how that would look?

>can't count the characters in an empty string
can't count the characters in a nil string

C# is highly underrated

Attached: Capture.jpg (543x154, 21K)

#include
#include
#include


int numberOfOccurences(wchar_t character, std::optional string) {
if(!string) return 0;
int i = 0;
for(auto c : *string) {
if(c == character) i++;
}
return i;
}

int main() {
std::cout

>count char in string function with string being optional

Attached: 1512918129610.jpg (227x250, 4K)

>throwing a new exception for this
Never going to make it, brainlet

sum(x == c for x in s)

I almost made a brand new OpIsAFaggot exception just for him

Yeah, might as well do it properly with an early return.

Attached: ds.png (1090x552, 37K)

fixed pastebin.com/puNkahBD

holy shit, that's ugly.

What don't you like there?

public static int OccurancesOf(this string s, char of) => s?.Count(c => c == of)??0;

>of
>in
wtf

Not bad.

> method name starting with capitalized letter
please kill yourself

all those degenerate std's triggers me

fn number_of_occurences(c: char, in: Option) -> usize {
in.map_or(0, |s| s.chars().filter(|&f| c == f).count())
}

Yeah you should use using declarations

% cat count.pl
#!/usr/bin/env perl
use feature 'say';

sub numberOfOccurences
{
my($c, $s) = @_;
scalar( ()= $s =~ /$c/g )
}

say numberOfOccurences 'a' => 'ur an faget';
% ./count.pl
2

Internal and external parameter names, makes for a prettier, more readable API.

fn count(rune: char, string: Option) -> u32 {
match string {
None => 0,
Some(s) => {
let mut result: u32 = 0;
for c in s.chars() {
if c == rune {
result += 1;
}
}
result
}
}
}

Jesus, at least use map and unwrap_or

>map
never. loop 4 lyf
>unwrap_or
ok you have a point

"in" is a reserved word so you have to change the second argument's name.

That will compile to basically the same thing as

I mean mapping the Option. You don't need to pattern match.

This is the idiomatic Rust version:
pub fn foo(s: Option, c: char) -> usize {
s.map_or(0, |s| s.chars().filter(|&c2| c == c2).count())
}
It is also the fastest: rust.godbolt.org/z/kzivqR

sub numberOfOccurences{ scalar map 0, $_[1]=~/$_[0]/g }
ez

Hmm, I can't read assembly but what do you have to say to this?
rust.godbolt.org/z/RYdv0I

>all that code for just counting occurances
lol rust

literally the same: rust.godbolt.org/z/4zKCOG

>sum(x == c for x in s)
>needing a loop

occurrence OF letter IN a sentence

rust.godbolt.org/z/oRAXiQ
Again, I can't read assembly but they look pretty similar

It's pretty damn minimal
rust.godbolt.org/z/GpFqAL

def numberOfOccurences(of: Char, in: String): Int = in.filter(_ == of).length

numberOfOccurences(of = 'f', in = "you're a faggot")

Looks like the C++ loop will do at least four characters at once (assuming a 32-bit wchar_t, more commonly 16-bit) due to SIMD.

Stop using the retarded pattern matching looping version. It's basically the same as the version that's a single line of code.

lol who says SLoC counts

I hear SIMD just landed in Rust couple months ago.

Explicit SIMD. The C++ version using SIMD is a compiler optimisation meaning that either:
1. Rust's compiler doesn't have enough information in the loop to determine that the SIMD optimisation can be done.
2. C++ optimisation is insane.

pub fn count(s: Option, c: char) -> usize {
s.into_iter().flat_map(|s| s.chars()).filter(|&d| d == c).count()
}
SIMD intrinsics were added to core/std. You could still use SIMD instructions before by using inline asm.

People that have to read and maintain your shitty code. In this case, the single line version is far more readable than your 13 line bloat.

Here is minimal.

godbolt.org/z/c_aonq

Yours is a god damn abomination. Jesus.

[..."ssstttrrrringgg"].reduce((acc,cur)=>(acc[cur]=(++acc[cur])||1,acc),{})


JavaScript rocks!

>godbolt.org/z/c_aonq

If you think this is equivalent to the Rust or the C++ version above you're just dumb.

>ur dum
Didn't expect anything else from rust zombies.

>null pointer is optional value
Get out, ctard

Attached: 1519264861280.png (645x729, 62K)

> post code that doesn't do the same thing
> hurr durr it's faster

>buggy, doesn't work with non-ASCII character
>It's a C application
clockwork

get rekt: rust.godbolt.org/z/BDUUix

It is an optional value in C. Just because your shitty language can't have it doesn't mean that all other are forbidden from using it.

Change char to wchar_t. there you go. Enjoy.

There you: godbolt.org/z/NRf0pF
Happy?

>no UTF-8
into the bin

>Change char to wchar_t

Still wouldn't work properly brainlet.

get rekt #2: rust.godbolt.org/z/109_6d

(word, char) => [...word].reduce((count, c) => c === char ? count + 1 : count, 0)

quicker one:
(word, char) => word.match(new RegExp(char,'g')).length

golf
[..."ssstttrrrringgg"].reduce((acc,cur)=>(acc[cur]=+!!acc[cur]+1,acc),{})

>Happy?
No you fucking retarded dolt.
Why are C tards literally illiterate?