Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 634 635 [636] 637 638 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887637 times)

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9525 on: May 16, 2016, 04:58:32 am »

Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9526 on: May 16, 2016, 05:13:01 am »

Yeah, I found the last one after some Googling, but...Y'know. Kinda weird.

IDK, it makes more sense for each new type to be able to convert itself to other common types, than for every class to have adapters for every possible type.

Shadowlord

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9527 on: May 16, 2016, 10:44:16 am »

The .NET version does neither, though: arrays (like other collections) implement the ICollection interface (which extends IEnumerable). As such, a constructor which takes an ICollection can depend on anything being passed to it having a Count property and GetEnumerator() method which is used to step through all its elements.
Logged
<Dakkan> There are human laws, and then there are laws of physics. I don't bike in the city because of the second.
Dwarf Fortress Map Archive

Mesa

  • Bay Watcher
  • Call me River.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9528 on: May 16, 2016, 11:31:30 am »

So out of my pure and utter curiosity I've dedided to at least familiarize myself with Lisp a little (Common Lisp specifically) because it's intriguing to me. The syntax is surprisingly cool.

However, I'm doing this tutorial and most things so far make a surprisng amount of sense to me - except this one thing here regarding variables. I don't know how the hell the `format` expression/function (I might be using wrong names here) accomplishes what it accomplishes and what its syntax is. I get what it outputs, but not what actually happens in it.
Code: (Common Lisp) [Select]
; global variables stuff
(setq x 10); set the x variable to 10
(setq y 20); set the y variable to 20
(format t "x = ~2d y = ~2d ~%" x y); ...output "x = 10 y = 20"? but how?
; what is the purpose of ~2d and ~%? why 'format t'?
; help
;below is the same but with 100 and 200 instead

(setq x 100)
(setq y 200)
(format t "x = ~2d y = ~2d" x y)
Code: (Shell output) [Select]
x = 10 y = 20
x = 100 y = 200

Like I get what happens with setq and such, but the syntax of `format` is what made me kinda stop. Maybe I'm reading too much into it, but I'd rather at least have some idea than have none then be stuck with it again later down the line. :r

(Or maybe I shouldn't be doing Lisp to begin with.)
Logged

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9529 on: May 16, 2016, 12:35:59 pm »

Learning Lisp is a great idea.

Quote
Code: [Select]
(format t "x = ~2d y = ~2d ~%" x y); ...output "x = 10 y = 20"? but how?
format is a bit like C's printf. It takes a format string which can contain format directives. These irectives always start with a tilde. You see these two ~2d? These tell the format function to insert a number in decimal representation with width 2 into the string at their position (the "d" says that it's a integer in decimal representation, the "2" says to reserve 2 places for the number). What number they insert at that position is determined by the further arguments to format. So, the first ~2d consumes the first argument after he format string, which is x, and the second ~2d consumes the second argument after the format string. It's just done in order.

Does that help?

You actually don't need to know all of this right now. format really provides a kind of domain specific language for formatting strings and this syntax shows up nowhere else. If you cannot get into it, just ignore it and wait until you're comfortable reading the HyperSpec. When you're comfortable with that, you can read all about format in there.
« Last Edit: May 16, 2016, 12:38:05 pm by Antsan »
Logged
Taste my Paci-Fist

Mesa

  • Bay Watcher
  • Call me River.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9530 on: May 16, 2016, 12:54:51 pm »

Learning Lisp is a great idea.

Quote
Code: [Select]
(format t "x = ~2d y = ~2d ~%" x y); ...output "x = 10 y = 20"? but how?
format is a bit like C's printf. It takes a format string which can contain format directives. These irectives always start with a tilde. You see these two ~2d? These tell the format function to insert a number in decimal representation with width 2 into the string at their position (the "d" says that it's a integer in decimal representation, the "2" says to reserve 2 places for the number). What number they insert at that position is determined by the further arguments to format. So, the first ~2d consumes the first argument after he format string, which is x, and the second ~2d consumes the second argument after the format string. It's just done in order.

Does that help?

You actually don't need to know all of this right now. format really provides a kind of domain specific language for formatting strings and this syntax shows up nowhere else. If you cannot get into it, just ignore it and wait until you're comfortable reading the HyperSpec. When you're comfortable with that, you can read all about format in there.

It does kind of help, yes. But what about ~% though? That doesn't even seem to output anything (well, for one there's no argument for it, though adding a third variable/argument didn't seem to change that output so whatever...) and the example in question didn't even put it in the second `format`.
Logged

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9531 on: May 16, 2016, 01:04:15 pm »

'~%' is the directive for the newline. It doesn't consume an argument because a newline is just a newline. Directives may consume any number of arguments, depending on their purpose.

I personally would also put it into the second format (because ending your output with a newline just seems like basic manners), but it isn't really required.
Logged
Taste my Paci-Fist

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9532 on: May 16, 2016, 01:17:23 pm »

Trying to write a basic character calculator program for pathfinder. An integral requirement of this is a method to generate random numbers. I have a couple functions that simulate rolling dice and seem to work perfectly in their own standalone program. When integrated into the character calculator program as c++ header files or cpp files, the program winds up getting the same number for every dice roll.

I'm pretty sure this is the result of improper seeding. But I don't see why it would work like it does in the standalone dice program.

Spoiler: diceRoller standalone (click to show/hide)

As far as I can see the program should reseed the rng whenever the rollDice functions are called. Which it seems to work in the standalone program, since it consistently displays a different output. (1d6 rolls a 5, then rolling a 1d6 again rolls a 1, for example, rather than another 5.)

Spoiler: diceRoller.h (click to show/hide)

But in the character calculator when trying to use the functions from the header file, the program consistently gets the same value from different calls to the diceroll function, so output for rolling dice becomes 5 5 5 5, or 3 3 3 3, or etc, the same number 4 times.


I don't see what I'm doing wrong here. I'm pretty new to using header files or #including programmer-written code from programmer written files.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Shadowlord

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9533 on: May 16, 2016, 01:52:52 pm »

And the reason why: If the time doesn't change, then you seed it with the same value, and so get the same result.
Logged
<Dakkan> There are human laws, and then there are laws of physics. I don't bike in the city because of the second.
Dwarf Fortress Map Archive

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9534 on: May 16, 2016, 03:30:28 pm »

And here I was thinking re-seeding it every time was a good thing.

And the reason why: If the time doesn't change, then you seed it with the same value, and so get the same result.
It occurred to me that if the for loop computed fast enough then the value returned by time would remain the same for several reseedings and so the same seed would be used multiple times, but I didn't think that could actually be right.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9535 on: May 16, 2016, 03:49:58 pm »

Learning Lisp is a great idea.
This right here. It might be worthwhile to note that the amount of use you actually get out of it is very heavily dependent on what fields/tasks you are going to be working in/on (in a lot of cases you'll never program in it, where on the other hand in some cases it'll be practically all that you use), but even if you never ever use it again just the exposure to a different paradigm of computing (and all of that recursion practice) will help broaden your way of thinking a bit and open up some new ideas and paths you didn't realize where there before.

Honestly everyone should, if they have the time, learn one functional language (like LISP or Scheme) and one declarative language (Prolog is a basic example) in addition to the common imperative paradigm languages like Java or C, simply for the exposure to those things. In a lot of cases they aren't going to be directly useful (unless you happen to work in a field such as AI or server resource allocation that leans heavily on those paradigms), but just the exposure to the different ways of thinking will help you approach problems better and be able to realize those cases where a slightly modified imperative approach towards one of the other paradigms (or heck, maybe a library/unit written in one of the other paradigms) will work much better than the straight imperative approach would.
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9536 on: May 16, 2016, 04:07:51 pm »

Quote
Honestly everyone should, if they have the time, learn one functional language (like LISP or Scheme) and one declarative language (Prolog is a basic example) in addition to the common imperative paradigm languages like Java or C, simply for the exposure to those things.
I'm going to be nitpicking here, because this kind of sets off the Lisper in me.

Lisp is not a functional language. It can be used as such, but if you're going to do that, you are going to miss its full potential. In addition to that, if you want a functional language, use Haskell instead – as a functional language, it is way better than any Lisp I know.
Lisps are, first and foremost, meta-programming languages. That is what sets them apart and makes them awesome.

In a Lisp you normally have a very direct connection between the code you write, the theory behind compilers and the ability to use this direct connection to your advantage to change the compilation process to your advantage. Functional programming techniques certainly help with that, but they are hardly what makes this approach.

Ahem. I hope you can forgive my fanboyism.
Logged
Taste my Paci-Fist

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9537 on: May 17, 2016, 05:39:40 am »

Lisp is not a functional language. It can be used as such, but if you're going to do that, you are going to miss its full potential. In addition to that, if you want a functional language, use Haskell instead – as a functional language, it is way better than any Lisp I know.
Lisps are, first and foremost, meta-programming languages. That is what sets them apart and makes them awesome.
I never said you had to be working in a purely functional paradigm. :P

Though honestly when the point of the exercise is to expose yourself to another paradigm it doesn't really matter if you "miss [the] full potential"; because the benefit you are trying to obtain isn't that of learning another language, it's being exposed to another paradigm. The goal I was implying here wasn't to walk out as a professional Lisper (or heck, even a great one), because if you want to learn a language for actual usage then your time would be much better spent with a more commonly used language like Ruby or Clojure to learn since the job demand for them is much much higher than that for common Lisp. The reasons to choose Lisp instead are that it, one, allows you access to both the functional programming paradigm and the more niche paradigm of meta-programming, and two, it's an old enough language with enough historical value that pretty much everyone has heard of it.

Lisp is a wonderful language with a very passionate community (often too passionate about some mighty trivial things, if you ask me :P), but short of a brief exposure to other paradigms of programming or in cases where a field uses it any time you spend in terms of something you are actually going to use sometime in the future is much better spent on learning another OO language (C, or Java, or Python), or on learning web programming (HTML+PHP, Javascript), or learning database programming (SQL), or learning a multi-purpose language (Perl, Ruby) or even learning a specialized language like R for statistical analysis or MATLAB for scientific/engineering work. All of those will pay back your time in terms of actual usability in the job world over many times more than Lisp ever will (again, barring that you are looking into a field that is full of other Lisp enthusiasts).

(Lisp's lower level of popularity, of course, has only a slight amount to do with the language itself, so I don't mean to imply that it's useless or anything [or that you shouldn't learn it "for fun" as opposed to future-focused reasons]. Much larger factors in its inability to become a mainstream language were the failure of the Lisp community to ever rally around a single standard [or even a few standards] like many other languages did, the prevalence of a free C compiler with many early operating systems, and the fact that it started itself out as a flagship language for a programming field that suffered a very severe hit in the early 90's due to over enthusiasm bursting a belief bubble and causing severe cutbacks, along with several other notable factors, not any direct fault of the language itself).
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9538 on: May 17, 2016, 06:00:34 am »

Fair enough. I think I didn't make my actual point, though.

Haskell's type system makes functional programming really great at times. It is kind of the only thing that I dearly miss in Common Lisp (I'd not want it to be mandatory, though). Of course there's Shen which has an even better type system, but in Shen I miss backquote syntax and quicklisp and that seems even more important.
« Last Edit: May 17, 2016, 06:03:23 am by Antsan »
Logged
Taste my Paci-Fist

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9539 on: May 18, 2016, 03:33:13 am »

Teaching myself regex's for something DF related. Only the last line after each BIND parses the way I want it to:
Code: (Regex) [Select]
/^\[BIND:([\w]+):([\w]+)\](.*)\n+(?:(\[(?!BIND:)[^\]+]+\])(.*)\n+)*/gm
Code: [Select]
[BIND:SELECT:REPEAT_NOT] comment
[SYM:0:Enter] comment
[SYM:0:Numpad Enter] comment
[BIND:SEC_SELECT:REPEAT_NOT]
[SYM:1:Enter]
[SYM:1:Numpad Enter]
[BIND:DESELECT:REPEAT_NOT]
[KEY:z]
[BIND:SELECT_ALL:REPEAT_NOT]
[SYM:1:Enter]
[SYM:1:Numpad Enter]
[BIND:DESELECT_ALL:REPEAT_NOT]
[KEY:Z]

Use the [Select] button and copy-paste them here to test. What am I doing wrong?

Edit: Alternatively, how would I break up each keybind segment into groups so I can parse the individual lines later?
See below post.
« Last Edit: May 18, 2016, 09:57:05 am by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?
Pages: 1 ... 634 635 [636] 637 638 ... 796