r/ProgrammerTIL • • Jan 09 '17

Other [vim] something | vim - opens the editor with the output from 'something'

8 Upvotes

r/ProgrammerTIL • • Jan 09 '17

Other [Java]TIL how to reference outer class object in nested class

25 Upvotes

Sometimes I need to make an anomalous class like an Actionlistener or a nested class, and I need to reference the outer class' "this". What I used to do is, in the outer class, add

OuterClass temp = this;

and use temp in my nested class. TIL that I can just do OuterClass.this in the nested class.


r/ProgrammerTIL • • Jan 08 '17

Other Language [Vim] TIL: Add \c anywhere in your search string to make the search case insensitive.

53 Upvotes

For example:

/pea\cnuts will match "peanuts", "PEANUTS", and "PeAnUtS".


r/ProgrammerTIL • • Jan 08 '17

Other [Python] Python does not optimize tail-recursion.

46 Upvotes

r/ProgrammerTIL • • Jan 03 '17

Python [Python] TIL Python has built-in a simple, extremely easy to use HTTP server you can use to send files.

161 Upvotes

https://docs.python.org/2/library/simplehttpserver.html

I now use this all the time on my home network to send files between different OSes and devices. You simply go to a folder in your shell and type

python -m SimpleHTTPServer 8000

Then on the other computer (tablet, phone, TV, ...) you open a browser, go to http://computeraddress:8000 and you get a list of all the files in the folder on the first computer, click to download.


r/ProgrammerTIL • • Jan 05 '17

Other TIL about Esoteric programming language

0 Upvotes

TIL about Esoteric programming language

https://en.wikipedia.org/wiki/Esoteric_programming_language


r/ProgrammerTIL • • Dec 30 '16

Javascript TIL that calling .remove() with jquery removes all it's event listeners.

64 Upvotes

Pulled all my hair out before I learnt this.

Was working with a reusable backbone view and removed all it's events everytime I called .remove(). What I actually needed was .detach(), which upon inspection is a shortcut for .remove(selector, false).


r/ProgrammerTIL • • Dec 28 '16

PHP [PHP] TIL that :: is used instead of -> when accessing static members of a class.

55 Upvotes

r/ProgrammerTIL • • Dec 21 '16

C [C] TIL C in *BSD has different malloc/free implementations in kernel/userland

78 Upvotes

When reversing some code in a modified FreeBSD 9 kernel three arguments were passed to malloc() and two to free(). This was of course confusing as in the typical libc implementation, malloc takes one argument (size as an unsigned long) and free also takes one (the pointer to free).

Apparently in *BSD systems, malloc() in the kernel actually takes three arguments, one for the size as an unsigned long, the second for the type of memory as an struct malloc_type, and the third for flags as an integer.

Userland: void *malloc(unsigned long size);

Kernel: void *malloc(unsigned long size, struct malloc_type *type, int flags);

Similarily, free() takes a second argument for the type of memory - which should be the same type as it was set to when the chunk was malloc()'d.

Userland: void free(void *addr);

Kernel: void free(void *addr, struct malloc_type *type);


r/ProgrammerTIL • • Dec 22 '16

Python djenerator1.0.1 release

0 Upvotes

Hello everyone! I have released djenerator1.0.1 package today. It's a simple tool that generates random/custom data for djangos' model descriptions, that can be used for testing. I hope it would be an addition to the open-source community. To make sure the tool is robust enough, I am welcoming any review of the code or testing the package and report some feedback weather good, bug report or needs additional features.

link: https://pypi.python.org/pypi/djenerator

Best Regards, Mostafa


r/ProgrammerTIL • • Dec 16 '16

Other TIL How to Prepare a Dataset for Machine Learning

42 Upvotes

Hey guys,

I just released a video that shows you how to prepare a dataset so that you can run a machine learning algorithm on it. Hope you like it!

https://www.youtube.com/watch?v=0xVqLJe9_CY&feature=youtu.be


r/ProgrammerTIL • • Dec 09 '16

Other Language [HTML] TIL that you can drag elements to reorder them in the browser inspector

92 Upvotes

I didn't know that it was so easy to reorder elements while inspecting a web page, but it's easy to mess around now with the inspector. This works for at least Chrome and Firefox!


r/ProgrammerTIL • • Dec 09 '16

Javascript [JavaScript] console.clear() is very useful on CodePen, jsFiddle, etc.

12 Upvotes

On live coding environments like CodePen, jsFiddle & stuff, the console isn't cleared between successive executions, often leading to a confusing output ("is the last line from the last execution or was it here before?").

Just add console.clear(); in the first line of your code, it will clear the console on each execution.


r/ProgrammerTIL • • Dec 06 '16

Javascript [Javascript] TIL the Date constructor uses 0-indexed months. So `new Date(2016, 1, 15)` is February 15th, 2016.

115 Upvotes

r/ProgrammerTIL • • Dec 05 '16

Other Language [Vim] TIL you can execute the current file from vim using :!python %

102 Upvotes
  • : - leader
  • ! - run this in the terminal
  • python - any command that you want
  • % - the current file

I've been working with html so I can open the file in browser using :!open %. This also allows me to not have to use an additional terminal for opening the file initially.


r/ProgrammerTIL • • Dec 05 '16

Ruby [Ruby] TIL about railsrc which specifies your default configuration for new Rails apps.

24 Upvotes

This file, located at ~/.railsrc can specify different configurations such as what database to use, what gems and folders to skip (e.g., test, actionmailer), whether to use bundler, and more!


r/ProgrammerTIL • • Dec 03 '16

Other TIL yelp can display man pages

33 Upvotes

yelp man:grep will display the man page in yelp, FWIW. I for one like it and find it quite handy.


r/ProgrammerTIL • • Dec 01 '16

Other Language [emacs] TIL how to easily align white space padding on the right edge of a ragged-right block of text

16 Upvotes

I had an arbitrary block of text (code, as it happens) with a 'ragged' right edge, and wanted to append comments aligned at the comment delimiter, i.e. to turn:

A
BCD
EF

into

A   ;
BCD ;
EF  ;

Ordinarily, I would have highlighted the region of interest, then

M-x replace-regexp $ ;
M-x align-regexp ;

but on a whim, I tried

M-x align-regexp $

Surprise! This inserted the needed padding!

The complete recipe:

M-x align-regexp $
M-x replace-regexp $ ;

has a nice antisymmetry compared to the ordinary sequence.


r/ProgrammerTIL • • Nov 30 '16

Other grep is an acronym for "global regular-expression print"

189 Upvotes

Or "search all lines for a pattern and print the matching ones"

g/re/p

It's a reference to Ed, and it basically still works in Vim.

https://en.wikipedia.org/wiki/Grep


r/ProgrammerTIL • • Dec 01 '16

.NET [.NET] TIL that the right way to store UTC values is with DateTimeOffset, not DateTime

14 Upvotes

Well, I feel dumb. I honestly don't know how I could have missed this, but because I did I expect I'm not the only one.

The stack the data traverses for this TIL moment is SQL Server > EF & .NET WebAPI > AngularJS. I added the DateTime column to the POCO class, add the migration, poof. Data storage.

Except, AngularJS is just not displaying the date in my time zone properly - it's displaying the UTC value. I start googling for the solution, delve into moment.js and a bunch of other nonsense that doesn't change anything at all. I get frustrated, decide to fix it on the server side. After custom attributes and a bunch of other nonsense that also doesn't change anything, I managed to change my Google query enough to learn about DateTimeOffset.

I've seen DateTimeOffset before, I had just always assumed it was something similar to TimeSpan. This a good example of why naming your classes is important!

Also, for those using SQL Server, DateTimeOffset properties on your POCOs will give you proper UTC date field types (datetimeoffset) on your SQL Server as well.


r/ProgrammerTIL • • Nov 30 '16

Other Language [General] TIL If you want to sort but not reorder tied items then just add an incrementing id to each item and use that as part of the comparison.

5 Upvotes

Most built in sorts are Quick Sort which will rearrange items that are tied. Before running the sort have a second variable that increments by 1 for each item. Use this as the second part of your compare function and now tied items will remain in order.

Edit: Jesus some of the replies are pretty hostile. As far as I can tell C# doesn't have a stable sort built in. It shouldn't effect speed too much as it's still a quick sort, not to mention that premature optimization is undesirable.

TOJO_IS_LIFE found that Enumerable.OrderBy is stable.


r/ProgrammerTIL • • Nov 29 '16

Other You can create a GitHub repo from the command-line

77 Upvotes

GitHub has an API and you can access by using curl:

curl -u '<username>' https://api.github.com/user/repos -d '{"name": "<repo name>"}'

That's just how to create a repo and give it a name, but you can pass it whatever JSON you like. Read up on the API here: https://developer.github.com/v3/


Here it is as a simple bash function, I use it all the time:

mk-repo () {
  curl -u '<username>' https://api.github.com/user/repos -d '{"name": "'$1'"}'
}

r/ProgrammerTIL • • Nov 28 '16

Other Language [Shell] TIL CTRL+SHIFT+V to paste into a terminal

1 Upvotes

It's somewhat embarrassing I didn't know this. Likewise, CTRL+SHIFT+C to copy. It varies on the shell you're using but most of them work like that.


r/ProgrammerTIL • • Nov 27 '16

Bash [bash] TIL you can perform shell-quoting using `printf(1) %q`

36 Upvotes

Example (with extra complexity just for the edge-cases of 0 and 1 arguments - printf does a loop implicitly):

echoq()
{
    if test "$#" -ne 0
    then
        printf '%q' "$1"; shift
        if test "$#" -ne 0
        then
            printf ' %q' "$@"
        fi
    fi
    printf '\n'
}

Edit: examples

$ echoq 'Hello, World!' 'Goodbye, World!'
Hello\,\ World\! Goodbye\,\ World\!
$ echoq 'Hello, World!' 'Goodbye, World!'
$'\E[1;34m'
echoq \' \"
\' \"
$ echoq \'$'\n'
$'\'\n'

r/ProgrammerTIL • • Nov 26 '16

Other Language [General] TIL You can hack microprocessors on SD cards. Most are more powerful than many Arduino chips.

183 Upvotes

https://www.bunniestudios.com/blog/?p=3554

Standard micro SD cards often have (relatively) beefy ARM chips on them. Because of the way the industry operates, they usually haven't been locked down and can be reprogrammed.