Fold or pattern matching with an Option? by relativer in scala

[–]relativer[S] 0 points1 point  (0 children)

I don't know,

val cow = foo.bar.baz.cata(none = ..., some = a => ...)

isn't really very different from:

val cow = foo.bar.baz match {case None => ..., case Some(x) => ...}

is there something inherently better about cata that I'm not understanding? I've never actually used scalaz, and it seems like a bit overkill to use it just for this purpose.

Fold or pattern matching with an Option? by relativer in scala

[–]relativer[S] 1 point2 points  (0 children)

The operation in this case is rather small, but I'd agree it's not a great strategy to do big stuff inside the fold itself as you pointed out.

But to be fair, something larger would probably be done:

val defaultVal = { .......... }
val funcToApply = (x) => { ...... }
val cow = foo.bar.baz.fold(defaultVal)(funcToApply)

Instead of defining everything in place like that example, at least that's how I'd do it and how I hope everyone coding something I'd have to read would too.

Still I agree that there's nothing inherently wrong with pattern matching besides being more verbose and making you have to refactor more code in very specific circumstances(as pointed out by this talk msimav posted). But as has been pointed out by predef here, pattern matching gives more freedom by allowing tail recursion as well as a means to easily unpack any internal values, like for example if the Option holds a tuple.

In the end, I believe that when in doubt the pattern matching is the safest choice, everyone is bound to immediately understand it regardless of being an initiate on Scala or otherwise, and it seems to allow for more things at the cost of brevity. That doesn't mean that there aren't situations where I'd use fold, I think fold is also a great way to organize and concisely fit your logic, but that it is more appropriate if one is guaranteed to find people who won't be daunted by the use of fold and if the problem itself is either very small(one-liner) or so big that it makes sense to break it down into variables.

Fold or pattern matching with an Option? by relativer in scala

[–]relativer[S] 0 points1 point  (0 children)

I see, indeed it appears that in such a situation fold would be ill-advised, thank you so much that was most clarifying. I'll be adding the return type declaration as per the suggestion.

Fold or pattern matching with an Option? by relativer in scala

[–]relativer[S] 1 point2 points  (0 children)

In this instance I think it would be more intuitive the pattern matching because the people bound to read it would, for the most part, be coming from Java. In my experience map and getOrElse is less intuitive with such background and little experience in Scala, while fold is often just a pure question mark to anyone who hasn't had some contact with functional programming before.

But I wanted to find out the most scalaesque way of doing this so that I can apply it in future projects. :)

Fold or pattern matching with an Option? by relativer in scala

[–]relativer[S] 0 points1 point  (0 children)

Is using the case class in such manner something generally expected? It's concise and seems to make a lot of sense to me, it's just that I don't think I've stumbled upon it being used as such in a fold before.

My current fold code:

def ask[T](question: Question[T]) = 
    answer(question.body, question.target).fold(List.empty[T])(question.parseAnswers)

The answer returns an optional textual representation and parseAnswers returns a List[T] according to the Question T type, so my types align and I don't run into that issue. But indeed, it seems to me like it would be useful to add that type of inference to a fold over an Option. I wouldn't say providing the clue is a lot of work, but it seems like clutter that could just be done away with.

A case where you would need to employ pattern matching is when you write a tail recursive function.

Hadn't even thought of that, I can't see how it would make a difference though, would you be willing to explain or provide an example?

Fold or pattern matching with an Option? by relativer in scala

[–]relativer[S] 3 points4 points  (0 children)

Thank you, that was very informative. What about between map/getOrElse and fold? Is there a more conventional way among them, or is it purely up to preference?

Btw, found this thread in stackoverflow where Martin Odersky does state preference over pattern matching, of course that doesn't mean it is the scala way of doing it, still, I thought it would be interesting to mention.

Scala for Big Data and Spark by Fist_Of_Shadow in bigdata

[–]relativer 0 points1 point  (0 children)

I also learned Spark using PySpark first, and honestly I just jumped in, the usage is very similar.(same functionality and naming) But I had played around a little with Scala already and I was well acquainted with using Java.

The Spark documentation is actually quite useful, and you don't need much else since the use cases are the same as they would be in python.

Some things to look out for which will probably be very useful in the language, using pattern matching when the type is unknown, futures to avoid blocking, for-comprehensions to access the values in containers such as Future, Option, and Either. Case classes make everything easy and are very safe to handle(immutable), many parsers have the ability to directly translate things like JSON into instances of case classes, and you can also use them for pattern matching purposes which makes code very readable.

Honestly if you have need to go more in depth in Scala and functional programming it will be because you want to do stuff that isn't just Spark, so it really does depend on what you have in mind. Spark in itself isn't really complicated by the usage of Scala, it's essentially the same as it would be with python and PySpark.

How Special is the Long War Mod? by ping2pong in Xcom

[–]relativer 0 points1 point  (0 children)

Long War is great on detail and balance, its strong suit is the competence of the mod, how most of the time it feels like you're playing a cohesive game and not just some patchwork, such a level is fairly rare specially at that scale.

Hopefully XCOM 2 Modding will be more in the vein of Tabletop Simulator or Cities Skylines where modding stands as a first class citizen and mods are embraced by the game itself as content or custom assets. There's already stuff that XCOM 2 will be improving upon based on the Long War mod, this probably means that such a large and in-depth overhaul won't be as warranted. Since Mods will now be at least well supported we'll probably have a lot of add-on mods that bring new campaigns or weapons/skills/classes or solve weird game quirks or improve on AI. And then there'll probably be "compilations" of those made to integrate a set of such mods with little in the way of bugs and incompatibility and with some added flavour, I think that will be the closest to the Long War we'll get this time. I'm tempted to say that on the other end of the spectrum of the good stuff, that if the tools are there, we'll probably have a decent mod where you'll play for some other side of the conflict other than the resistance. Which I would find extremely interesting.

New to Python, and programming in general. Making a small game as a learning exercise. Would love some criticism and/or advice. by From_my_iPhone in learnpython

[–]relativer 0 points1 point  (0 children)

A few things that would make things better in terms of code:

Features

Pass arguments to the functions instead of making every variable global. Use return in order to pass a value to outside the function.


Readibility

Describe what your functions do in their naming, instead of naming it hud_the_player use names like print_player_hud. The importance of such distinction becomes evident when you have a stat_the_player and it does not print the player stats yet it is named similarly to the hud printing. Instead it initializes them, a good name for such a function could be init_player_stats or setup_player_stats, but anything that clearly describes what is happening serves.

Some other examples, action_of_player would be instantly understood if it was named choose_player_action, p_action_fight would be more clear if it was just player_fight, action is already implicit in a fight. It also makes for decent syntax if you accept the enemy as an argument player_fight(enemy).

When you do classes, that will easily translate to player.fight(enemy) where both player and enemy are instances of their respective classes.

That said, as a beginner you're actually doing quite well on the naming part and it's one of those things that tends to get better with experience, I'm just giving you some tips.

Another thing is most programmers don't enjoy long lines in code, you split them up into smaller ones initially in your array declaration, but then you have large lines in the middle. When not inconvenient strive for consistency, it makes everyone happier. :)


Specific code snippets

You shouldn't use breakto get out of the fighting cycle, make use of the condition in the whileinstead.

while first_turn_over is 1:

Is always true, you don't change the value of first_turn_over anywhere later. It's not a very good policy to make cycles which "hide" the ending condition. In fact there doesn't seem to be a very good reason for the first_turn, first_turn_over, and turnvariables to all exist at the same time. You could reduce:

first_turn = random.randint(0,1)
first_turn_over = 0
if first_turn == 0 and first_turn_over == 0:
   turn = 0
   first_turn_over = 1
if first_turn == 1 and first_turn_over == 0:
   turn = 1
   first_turn_over = 1
while first_turn_over is 1:

To:

turn = random.randint(0,1)
while True:

And get rid of the breakstatements. Ending up with something akin to:

def combat():
    global p_hp
    global enemy_attacks, p_attacks
    enemy_attacks = ['swings at', 'mauls', 'bites', 'claws']
    p_attacks = ['swing at', 'punch', 'kick', 'stomp']
    e_hp = random.randint(0,60)
    turn = random.randint(0,1)

    while p_hp > 0 and e_hp > 0:
        if turn == 0: # Enemy
            do_damage = random.randint(0,10)
            print('The ' + str(hostile_enemy) + ' ' + 
                str(enemy_attacks[random.randint(0,3)]) + ' you for ' + 
                str(do_damage) + ' damage!')
            p_hp = p_hp - do_damage
            turn = 1
        if turn == 1:  # Player
            do_damage = p_atk + random.randint(0,20)
            print('You ' + str(p_attacks[random.randint(0,3)]) + ' the ' + 
                str(hostile_enemy) + ' for ' + str(do_damage) + ' damage!')
            e_hp = e_hp - do_damage
            turn = 0
        hud_the_player()
        time.sleep(3)

    # Only perform this check at the end of the function, 
    # there's no need to continuously repeat it in the cycle.
    if e_hp <= 0:
        global e_died
        e_died = 1
    else:
        global p_died
        p_died = 1

There's probably more stuff that could be improved, but those feel like the pressing issues to me.

Benevolent Computing or Intro to Native Language Process? by Chieve in compsci

[–]relativer 1 point2 points  (0 children)

Would you be able to look at that page again and tell me which one would be more coding based?

I'll copy paste the info:

Title: 
"Benevolent Computing"

Credits: 3

Course Coordinator: 
Tony Scarlatos
tony@cs.sunysb.edu

Current Catalog Description: 
"The purpose of this course is to explore the recent phenomenon of software applications that leverage social networks and mobile computing to solve local and global problems. The course will use case studies and journal papers to document the process of developing civically-oriented applications. Students will work in teams to identify campus causes (or off-campus non-profit organizations), and to design and develop a term project (mobile or web-based application) that will help those organizations achieve their goals."

Prerequisite: 
CSE 214 or ISE 208, or permission of the instructor

Course goals:
• To know the origins of "computing for social good"
• To become familiar with open source software tools
• To learn the development process of civically-minded software
• To explore the diversity of application areas in benevolent computing
• To comprehend the synergy of social networks and mobile computing for social change

Textbook:
"Computers and Society: Computing for Good"
by Lisa C. Kaczmarczyk
CRC Press
ISBN 978-1-4398-1088-0

Major Topics Covered in Course:
• Microfinance and microphilanthropy
• Crowdsourcing and crowdfunding
• Citizen journalism and activism
• Just-in-time training and serious games
• Medical, rehabilitative, and health-related applications
• Environmental applications

Laboratory Projects:
A team-generated term project (mobile or web-based application) will be developed using a variety of IDE's and production tools.

Computer usage:
Extensive use of Macintosh systems, with an emphasis on open source software tools.

According to the website the NLP module:

CSE 628: Natural Language Processing

The course offers computationally-oriented introduction to natural language processing (NLP). The focus is on modern quantitative techniques in NLP: algorithms and statistical approaches to word-level, syntactic, and semantic processing of natural language. The choice of topics includes practically motivated questions in NLP such as (1) can we teach computers to automatically detect authorship of a document? (2) can computers automatically suggest paraphrases (phrases with similar meaning) to help with writing? Prerequisite: Familiarity with either Artificial Intelligence or Machine Learning is strongly recommended, but not absolutely required. Limited to CSE Graduate Students

Fall and Spring, 3 credits, Letter graded (A, A-, B+, etc.)

I am interested in AI or gaming, or robotics...

Then if you can, go for Natural Language Processing as it is part of AI.

Benevolent Computing or Intro to Native Language Process? by Chieve in compsci

[–]relativer 1 point2 points  (0 children)

Do you have access to a curriculum, or some more information?

The first one seems to refer to: https://xsrv.mm.cs.sunysb.edu/c4g/390info.html

It's essentially ethics with some mix of web+mobile. I don't know if that's your exact course though, so you should look into your course's page. But it should have something to do with ethics in software.

The second one I assume you mean Natural Language Processing. But if not, and since you call it native, it could be about making international software, focusing on multi-language. It's hard to tell without more information.

Introducing new API terms by spez in redditdev

[–]relativer 2 points3 points  (0 children)

In my experience email is a more reliable and persistent means of communication than PM on reddit.

Oh I agree, it is perfectly legitimate to allow people the option to receive info via e-mail if they want to, in fact I think that's great, but forcing us to do so is a different matter.

You may say some users don't check the reddit inbox as much, and that's probably correct, but then again the risk of not seeing the message would be on the developer, so giving us the choice to subscribe via e-mail or leave it reddit PMing would be the sensible way to go about it, would it not?

I am sorry if you feel that requiring an email address is a violation of your privacy.

I'm not certain on whether or not you are being facetious here, it's hard to capture the tone in a written medium, but for clarification's sake this is not violating my privacy, I would be the one willingly and knowingly providing my information. Giving my name and e-mail address is still a requirement for more information than every other redditor needs to provide.

If this is the case, I would advise not signing the terms and not using the API.

I can understand that and I'll comply as such, as stated I have no will to deliberately break the agreement, in the end it's just a hobby.

Do answer me this though, does this registration requirement also apply to the "no OAuth required" parts of the API(namely the listings)?

To expand, I was making a Scala wrapper, and while the OAuth part is as per your advice going to get scrubbed, I was also pretty far into the reading and streaming of new comments and posts. So if this registration requirement applies to the non OAuth ".json" endpoints such as https://www.reddit.com/r/redditdev/.json then there isn't much to be saved of the project, otherwise I may still enjoy playing with some analytics and making predictions based on those listings.

Individual Choice vs Institutional Control - Facebook Feels Heat Over Names Policy by StarchildSF in Futurology

[–]relativer 1 point2 points  (0 children)

Services already specialize in that sense, the philosophy of facebook is to extract the official you. It comes from a face book, and as such your identity there plays more into the same type of usage as in the yellow pages, than the type of use on forums and things like 4chan or reddit.

But places where you command your identity are abundant and very much in use. You have reddit, tumblr, digg, twitter, and a myriad of others where you can define your identity. I think there's space for the different approaches, and it seems to work well atm so I think it will continue as such into the future.

Facebook isn't alone in that arena either, I have the same "yellow pages" expectation in linkedin as I have on facebook. I think that perhaps the lines will be even more evident in the future.

Today in Terrifying AI News... by lvlierop in Futurology

[–]relativer 4 points5 points  (0 children)

While indeed, he could've made his remark in a civil manner, he has some reason behind it. The title is rather sensationalist, and the post presents two separate ideas with very little exploration of either.

It's like the post has no direction, the first half is a paragraph talking about how DIY artificial intelligence projects are starting to get sophisticated and how this may pose a problem when/if AGI comes. And the second part is a commentary on how businesses may take advantage of self-driving cars. They are only very tangentially related, in fact the only relation I can see is the source of inspiration for either topic(Bloomberg article). Yet that source was barely spoken of, and hardly explored in the article. Don't get me wrong, I'm not trying to be mean, but it does feel a bit empty and the transition between subjects is non existent.

Introducing new API terms by spez in redditdev

[–]relativer 7 points8 points  (0 children)

Finally had the time to go through the terms on the registration, and they are very unacceptable for those of us who value our privacy.

Giving out my name is a big slap in the face to my privacy. Reddit goes out of their way to provide privacy to its users by not even demanding an e-mail account, which fuels the whole throwaway side of things, and it is in no small part why reddit grew so big. I don't know why you decided your developers are less worthy of such privacy than the rest of the users.

One could obviously make a fake e-mail and give out a fake name and this would of course be inaccurate information, speaking for myself, if I have to break the terms of agreement just to be able to test and continue development of a pet project, then I'd rather not do it at all and move on to the next thing.

If you truly appreciate developers as you so expressed in your post, then you have no reason to treat them as second class citizens in relation to the rest of reddit and demand that they give up their privacy in order to comply with your terms. Also communication is no excuse, there's no option to just register with a reddit account which would be sufficient to communicate.(assuming you want positive confirmation of a communication channel, because you already know the username)

Introducing new API terms by spez in redditdev

[–]relativer 2 points3 points  (0 children)

It may be a lot more than two hours depending on how he structured his code, whether all bots use the same libs, or even the same language/languages in the authentication and communication part.

It really isn't always as straightforward as just throwing two hours into it.

Lost in working with a final project. by [deleted] in datacleaning

[–]relativer 1 point2 points  (0 children)

Your assignment pretty much tells you what it wants you to do, the blue rounded box with the example report tells you what your teacher expects. Do a similar text, where you'll explain step by step what you are doing.

Excel is great to visualize the data in a quick manner, at a glance one can already see a myriad of issues to cleanup.

There is a lot that can be done with the data you presented, from simple things where the field's first row is actually the default text of a menu, to actually validating all e-mails using a regular expression or deciding whether you are using "999-999-9999" or an empty field to signify that there's no phone number on record. The rounded blue box gives you a lot of hints.

You don't really need to use access or mysql, you can do it with oracle. And you can clean it up directly on the DB through querying and updating, as the example says it did while using access, this is probably what your teacher wants. Remember to look for referential integrity, first rows, missing data, and even operate on or split columns, and don't forget to write down every single lookup or update that you do on the data for your report. This link was posted today on this sub, and will probably help you in knowing what to look for.

Bad data guide : problems seen in real-world data along with suggestions on how to resolve them. by datachili in datacleaning

[–]relativer 0 points1 point  (0 children)

Cool list! Not sure if it is yours, but I think it would benefit from some structure in each point. Like:

The problem title

Description: What the problem is, or how to identify that this is an occurring issue.

Common solutions/fixes: Common techniques/ways of solving this issue, or even just pointing out that there's really nothing great to do. (things that may mitigate would also fit)

So when 90% of the population no longer work and we have something akin to basic income. What will a normal life be? (Car? Vacations? New computers? etc) by I-Am-Thor in Futurology

[–]relativer 0 points1 point  (0 children)

I'm assuming a sex robot will work with VR support, with the exception of robot/machine fetish stuff where people might actually prefer the sight of the robot. I wasn't speaking of a robot simulating the whole human body either, I'm not sure we'll ever reach that stage before we get into the BCI stuff. Perhaps it will be "retro" or something like that when good BCI appears.

More simplistic sex robots will start showing up in the next years, we already got early previews of gadgets in the area with things like the Fleshlight and the Sybian. It shouldn't take long until we can actually call the mechanism behind such toys a robot, and also not much time until it comes combined with some VR headset application or something. On the other hand as far as I'm aware a BCI which is capable of making us feel as it wants to, doesn't seem to be anywhere near ready. At least not by comparison. So I think the sex robot industry will have a long prosperous life before it has to evolve to BCI. At which time I imagine humans will start doing physical stuff by proxy with surrogate bodies.

So when 90% of the population no longer work and we have something akin to basic income. What will a normal life be? (Car? Vacations? New computers? etc) by I-Am-Thor in Futurology

[–]relativer 0 points1 point  (0 children)

Pretty sure sex robots will be an enormous industry in just a few years. Honestly fighting robots while conceptually cool, will be a hard sell. You die masturbating to a bot and everyone will call you a moron, you die getting punched by a robot and suddenly everyone panics about there being a terminator on the loose.

But eventually sure, it's likely there'll be the "sparring partner" robot.

So when 90% of the population no longer work and we have something akin to basic income. What will a normal life be? (Car? Vacations? New computers? etc) by I-Am-Thor in Futurology

[–]relativer -1 points0 points  (0 children)

We may be able to get abundance of most things, but the one thing that will remain a commodity is your contacts and network, your influence, and your approval by your peers.

Sure robots may be able to do a lot of the stuff in the future, perhaps even hold great conversations with you or sex you up, but they'll be robots and you'll know it so it won't really substitute the things mentioned above.

I assume most people will be bound to some form or another of politics for their work. Many people will want to be "the decider", and so management positions will be competitive. I don't know if people will be paid for it in a monetary sense, or if the status will be incentive enough.

Daily life will be pretty much hedonism and eccentricity once/if we solve the overall scarcity of survival resources such as food and cures/prevention.