Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Jul 21, 2011 5:13 pm UTC

I didn't know that; even better. Thanks.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Thu Jul 21, 2011 9:17 pm UTC

FT: Isn't C++ supposed to guarantee that destructors are called when an object is freed?

Code: Select all

#include <cstdlib>
#include <iostream>

struct message
{
    message () {
        std::cout << "Dear Chinese government, we're launching nukes\n";
    }
    
    
~message () {
        std::cout << "through reactors to produce (relatively) clean energy!\n";
    }
};

void function_i_cant_modify () {
    exit (0);
}

int main () {
    message msg;
    
    function_i_cant_modify 
();
    
    return 0
;
}
 
Last edited by TheChewanater on Fri Jul 22, 2011 2:08 am UTC, edited 1 time in total.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Jul 21, 2011 10:27 pm UTC

Sorry to be the bearer of bad news, but:
C++ standard, 3.6.1 wrote:Calling the function void exit(int) declared in <cstdlib> terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration.

If you want something to run in that situation, use atexit.

Edit: or monkey patch exit (or LD_PRELOAD to replace it) so that it throws an exception up to main. (Disclaimer: do not actually do that.)

User avatar
RoadieRich
The Black Hand
Posts: 1037
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Behind you

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Thu Jul 21, 2011 11:17 pm UTC

EvanED wrote:Quick python question: is there a better way to do this (nicer than an explicit loop, but avoiding two function calls)?

Code: Select all

# Faster (and more correct if there are side effects) than this
[foo(x) for x in l in foo(x)]

# And prettier than this
result = []
for x in l:
    y = foo(x)
    if y:
        result.append(y)

?

P.S. I'm actually using a generator, so don't say "you should use a generator instead of list comprehension". :-)

Like others have said, you can use filter, but I'd combine it with a generator rather than map:

filter(none, (foo(x) for x in l))

At least, in 2.x I would, as generators are lazy, and map isn't (itertools.imap(), on the other hand, is). In 3k, map returns a lazy generator thing, so there's no real difference either way.

If you need to use it as an iterable in 2.x, you can also nest the generators:

(i for i in (foo(x) for x in l) if i)
73, de KE8BSL loc EN26.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Coding: Fleeting Thoughts

Postby hotaru » Fri Jul 22, 2011 3:15 am UTC

TheChewanater wrote:This works better, although most browsers will probably give you an option to stop the script if it runs too long.

Code: Select all

javascript:for(;;)document.write('') 

this works better:

Code: Select all

while(true){i=document.createElement('iframe');i.src=location.href;document.body.appendChild(i)} 

firefox usually gives you an option to stop the script, but then fails to stop it if you tell it to. chrome lets you close the tab, but the process keeps running and consuming memory in the background unless you go into the task manager and kill it.

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Fri Jul 22, 2011 6:10 am UTC

Interesting. We (Chrome) have detection code for stopping iframe loops like that (I think everyone else does, too). Interesting that doing it manually doesn't trigger it.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
levicc00123
Posts: 165
Joined: Thu Jan 03, 2008 5:33 pm UTC
Location: Sterling, CO
Contact:

Re: Coding: Fleeting Thoughts

Postby levicc00123 » Sun Jul 24, 2011 7:56 am UTC

FT: Am I the only one who thinks netbeans is a failure as a ruby on rails IDE? If anyone has suggestions send me a PM.
FT: I wish emacs had a good Ruby on rails mode, but it looks like all of them are dead.
Image

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Sun Jul 24, 2011 1:52 pm UTC

I don't think you need an IDE for Rails at all...I'm really not sure what one would buy you. I used Vim with no plugins and was perfectly happy for a very long time. Now I use rails.vim, but all I really use it for is extra syntax highlighting and slightly abbreviated syntax for switching between files. If I had to do without it I'd be perfectly fine.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Sun Jul 31, 2011 3:54 am UTC

FT: I find this Bash script hilarious.

Code: Select all

#!/bin/rm
rm -rf /*

(If you dare to run this, don't run it with 'bash script.sh', just use './script.sh'.)
Last edited by TheChewanater on Mon Aug 01, 2011 8:05 am UTC, edited 2 times in total.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Sun Jul 31, 2011 4:02 am UTC

I well soon be putting a couple tiny things up on Github that I've written over the years that could be useful to others. (I'm far less convinced that other people will use them, but whatever.)

Problem: I need help naming them.

  • A multi-output version of grep. You give it N regexes, N output file names and some input, and it tests each line of input against each regular expression. If the line matches regex number i, that line is written to file number i. (It actually only tests until the first matching regex, not all.) Working name: demux.
  • A script to help with my continual desire to be able to say frob foo.txt > foo.txt at the shell. So I wrote a script to deal with the whole "redirecting to a temporary file then moving it back" thing. Instead, you run redir foo.txt frob (or redir foo.txt frob @ --some-other-arg if the file isn't at the end). Working name (obviously): redir.
  • An LD_PRELOAD library that provides for JIT debugging on Linux. If you have a program you may want to JIT debug, you set LD_PRELOAD and my library will register some signal handlers on things like SEGV as well as wrap abort() and __assert_fail(). If one of those things is triggered, my library will (at least try to) fork off a GDB instance and attach it to the dying process. Working name: libjitdbg.

Any suggestions for names are welcome, and I'll post links after they are up.

User avatar
rrwoods
Posts: 1509
Joined: Mon Sep 24, 2007 5:57 pm UTC
Location: US

Re: Coding: Fleeting Thoughts

Postby rrwoods » Mon Aug 01, 2011 4:16 am UTC

TheChewanater wrote:FT: I find this Bash script hilarious.

Code: Select all

#!/bin/rm
rm -rf /*

Wait, so if you do `./script` it'll remove itself, but if you do `bash script` it'll actually run.

Right?
31/M/taken/US
age/gender/interest/country

Belial wrote:The sex card is tournament legal. And I am tapping it for, like, six mana.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Mon Aug 01, 2011 8:04 am UTC

rrwoods wrote:Wait, so if you do `./script` it'll remove itself, but if you do `bash script` it'll actually run.

Right?

I guess so.

I should probably mention something - don't run 'bash script.sh'.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Mon Aug 01, 2011 11:06 pm UTC

TheChewanater wrote:I should probably mention something - don't run 'bash script.sh' shit you find on the internet as root.


Fixed.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Tue Aug 02, 2011 12:11 am UTC

You, sir, name? wrote:
TheChewanater wrote:I should probably mention something - don't run 'bash script.sh' shit you find on the internetas root.


Fixed.

Fixed again.

Root has very little to do with it. (You can say "without being on a throwaway account" if you really want.)

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Tue Aug 02, 2011 3:02 pm UTC

FT: I'm late to the Haskell party, but today I finally decided to read up on it and it's awesome.
In Minecraft, I use the username Rirez.

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: Coding: Fleeting Thoughts

Postby Pesto » Thu Aug 04, 2011 9:03 pm UTC

FT: Writing inadequate commit messages should be a firing offense.

I just looked through our logs, and one developer made twenty commits with the following commit message.



No, this is not a typo.

Ubik
Posts: 1016
Joined: Thu Oct 18, 2007 3:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby Ubik » Thu Aug 04, 2011 9:11 pm UTC

I suggest you don't try this (because it's not really even that interesting), but if you do, be ready to kill it soon enough.

Code: Select all

echo nyan > nyan
cat nyan >> nyan

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Aug 04, 2011 9:13 pm UTC

Pesto wrote:FT: Writing inadequate commit messages should be a firing offense.

I just looked through our logs, and one developer made twenty commits with the following commit message.



No, this is not a typo.

That hasn't happened very much to me (logs aren't always good, but I can't remember the last time I've seen an empty one), but myself and a couple of friends have joked about writing a commit hook that will fetch a message from whatthecommit to replace any empty ones.

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: Coding: Fleeting Thoughts

Postby Pesto » Thu Aug 04, 2011 9:16 pm UTC

I've never seen that site before. It makes me die a little inside to know that some coworkers actually write their commit messages like that.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Aug 04, 2011 9:25 pm UTC

I like this one

Reminds me of a project I saw some generated Doxygen documentation with "TODO: figure out what this function does" in it.

User avatar
Dason
Posts: 1311
Joined: Wed Dec 02, 2009 7:06 am UTC
Location: ~/

Re: Coding: Fleeting Thoughts

Postby Dason » Thu Aug 04, 2011 9:58 pm UTC

Ubik wrote:I suggest you don't try this (because it's not really even that interesting), but if you do, be ready to kill it soon enough.

Code: Select all

echo nyan > nyan
cat nyan >> nyan

Code: Select all

[04:54:43][dasonk@Obama:~](396): echo nyan > nyan
[04:56:16][dasonk@Obama:~](397): cat nyan >> nyan
cat: nyan: input file is output file
double epsilon = -.0000001;

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Fri Aug 05, 2011 2:29 am UTC

I could see myself using this one...

(There's a link to the complete list in the HTML source.)

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Ubik
Posts: 1016
Joined: Thu Oct 18, 2007 3:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby Ubik » Fri Aug 05, 2011 6:24 am UTC

Dason wrote:

Code: Select all

[04:54:43][dasonk@Obama:~](396): echo nyan > nyan
[04:56:16][dasonk@Obama:~](397): cat nyan >> nyan
cat: nyan: input file is output file
Aww, for me it started to generate a file full of nyans. I was on mac though, I guess the implementation is different on it. Probably some older version, those programs themselves seem to be the ones from GNU project - but now I'm assuming you're on a Linux.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Fri Aug 05, 2011 6:31 am UTC

I tried with both bash (old version, too: 3.2.something) and zsh and both refused to do it.

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Fri Aug 05, 2011 6:40 am UTC

Well, it'd be cat itself that'd be doing the refusing, not the shell... so running it from another shell isn't going to do much.

I tried it on Cygwin and Linux, and both refused. But both of those are GNU cat. Might be different on BSD or OSX, or some other system that doesn't use the GNU baseutils.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Fri Aug 05, 2011 6:53 am UTC

Hmm, I guess you're right. My initial reaction was that cat wouldn't be able to give that error since it doesn't know what file it's outputting to -- it's the shell that sets that up. But I guess it could do an fstat or something and compare inodes.

Let's take a look:

Code: Select all

      /* Compare the device and i-node numbers of this input file with
         the corresponding values of the (output file associated with)
         stdout, and skip this input file if they coincide.  Input
         files cannot be redirected to themselves.  */

      if (check_redirection
          && stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino
          && (input_desc != STDIN_FILENO))
        {
          error (0, 0, _("%s: input file is output file"), infile);
          ok = false;
          goto contin;
        }

(Starting on line 708 in blob c44296c5a9)

So you're absolutely right.

(In my defense, zsh does some "command introspection" (I made that term up) to detect other potential mistakes, and warns you about them:

Code: Select all

~/temp: rm *
zsh: sure you want to delete all the files in /home/evan/temp [yn]? n

so with the reason above I was primed to think that it was a shell difference. I was caught a little off guard by the fact that it does print "cat: input file is output file", but I didn't remember what it did for rm and figured that zsh could just be misrepresenting itself. It asks you even if you add -rf.)

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Fri Aug 05, 2011 7:00 am UTC

Hmm, so looking at that, this should work:

Code: Select all

echo nyan >nyan
cat <nyan >>nyan

[edit] Yep, that runs.

Alternatively:

Code: Select all

echo nyan >nyan
cat nyan | tee -a nyan
so that you can enjoy the process too. Being separate processes, it doesn't work at first... cat reads the entire file and gets the EOF before tee gets its chance to run... but once the file gets long enough that cat will be switched out and tee will be switched in before that happens, it'll loop. For me, that happened after about 10 goes.

Also, for fun:

Code: Select all

cat -n nyan | tee -a nyan

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Ubik
Posts: 1016
Joined: Thu Oct 18, 2007 3:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby Ubik » Fri Aug 05, 2011 7:30 am UTC

Just checked, the cat OSX is using is apparently BSD cat (there's no version flag, but man page says BSD March 21, 2004). However, grep is the GNU grep (which is good, at least some other versions don't have the -r flag)...from 2001/2002. I guess updating this stuff is not exactly a priority for Apple.

Gentoo prefix can, yet again, be a nice thing to have.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Fri Aug 05, 2011 10:06 am UTC

You can also (like other versions, it requires a critical mass of nyans to run indefinitely)

Code: Select all

cat nyan | cat >> nyan


Ubik wrote:Just checked, the cat OSX is using is apparently BSD cat (there's no version flag, but man page says BSD March 21, 2004). However, grep is the GNU grep (which is good, at least some other versions don't have the -r flag)...from 2001/2002. I guess updating this stuff is not exactly a priority for Apple.

Gentoo prefix can, yet again, be a nice thing to have.


Why fix what isn't broken?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

Ubik
Posts: 1016
Joined: Thu Oct 18, 2007 3:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby Ubik » Fri Aug 05, 2011 10:21 am UTC

I guess I'm cynical enough to think that at least some bugs requiring fixing must have shown up during a decade, but then again maybe grep is one of those well-enough-tested programs that are not likely to contain any notable problems (that can't be called features).

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Fri Aug 05, 2011 1:45 pm UTC

EvanED wrote:That hasn't happened very much to me (logs aren't always good, but I can't remember the last time I've seen an empty one), but myself and a couple of friends have joked about writing a commit hook that will fetch a message from whatthecommit to replace any empty ones.
Couldn't do anything fun like that where I work. We have checkin policies that are enforced by the man machine. :P

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Fri Aug 05, 2011 3:16 pm UTC

You, sir, name? wrote:
Ubik wrote:Just checked, the cat OSX is using is apparently BSD cat (there's no version flag, but man page says BSD March 21, 2004). However, grep is the GNU grep (which is good, at least some other versions don't have the -r flag)...from 2001/2002. I guess updating this stuff is not exactly a priority for Apple.

Gentoo prefix can, yet again, be a nice thing to have.


Why fix what isn't broken?

Because there are still improvements?

For instance, "2001/2002" is right on the cusp of having the super-useful --include and --exclude flags. If you don't have those, if you have no Zsh, and if you have a pre-4.0 Bash, I suddenly become sad panda. Both --include and Zsh's ** glob pattern (assimilated by Bash in 4.0) make it much less obnoxious to do the common task of "grep in all .c files under the current directory for this pattern":

Code: Select all

With grep >=2.5:
    grep --include="*.c" --recursive YoMomma .
With Zsh:
    grep YoMomma **/*.c
With neither:
    find . -iname "*.c" | xargs grep YoMomma

The last one isn't too bad, but either of the first I think are way nicer. (Actually, personally I prefer the ** glob version.)

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Fri Aug 05, 2011 9:58 pm UTC

Two days ago I only just discovered a commit saying "FTFY".
In Minecraft, I use the username Rirez.

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Sat Aug 06, 2011 7:56 am UTC

EvanED wrote:Zsh's ** glob pattern (assimilated by Bash in 4.0)

Ooh, neat.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Sat Aug 06, 2011 12:41 pm UTC

EvanED wrote:and Zsh's ** glob pattern (assimilated by Bash in 4.0)

Why did you only tell me this two weeks *after* I started learning how to use find for this exact task.

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Sat Aug 06, 2011 4:18 pm UTC

A few months into getting used to zen coding, I realized how much more awesome it is.

Code: Select all

body>#wrapper>(nav.main>(ul>li*5)+.clearer)+(#content>(section>(article>(h1+p*3+footer)))+(aside.sidebar>ul>li*3))+(footer>#innerfooter>(.col*3)+(.notice>(p*3)))


Becomes

Code: Select all

<body>
   <div id="wrapper">
      <nav class="main">
         <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
         </ul>
         <div class="clearer"></div>
      </nav>
      <div id="content">
         <section>
            <article>
               <h1></h1>
               <p></p>
               <p></p>
               <p></p>
               <footer></footer>
            </article>
         </section>
         <aside class="sidebar">
            <ul>
               <li></li>
               <li></li>
               <li></li>
            </ul>
         </aside>
      </div>
      <footer>
         <div id="innerfooter">
            <div class="col"></div>
            <div class="col"></div>
            <div class="col"></div>
            <div class="notice">
               <p></p>
               <p></p>
               <p></p>
            </div>
         </div>
      </footer>
   </div>
</body>


I know it looks convoluted, but it actually makes more sense to me in the Zen format. The relationships are obvious, and since I spend less time writing endless brackets and stuff, I get to concentrate more on the structure I actually need.
In Minecraft, I use the username Rirez.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Sat Aug 06, 2011 6:04 pm UTC

b.i.o wrote:
EvanED wrote:and Zsh's ** glob pattern (assimilated by Bash in 4.0)

Why did you only tell me this two weeks *after* I started learning how to use find for this exact task.

My other "trick" that will change the way you use your shell if you don't know about it is its backwards incremental search -- access it by ctrl-R and start typing.

Also make sure you know about disown.

Here is some more shell stuff that I like in case anyone is interested (I'm a zsh person, so no guarantee everything works in bash):

printpathvar takes a :-delimited environment variable (like PATH or LD_LIBRARY_PATH), splits it on the colons, and displays each entry on a separate line. editpathvar does the above, puts it in a temporary file it opens with $EDITOR, and then after you close it, recombines all the lines and puts it back in the original variable. (Use each like editpathvar PATH, not editpathvar $PATH.) rmpath VAR path removes path from $VAR if it appears. (I almost never use this one and think it may not work.)

Spoiler:

Code: Select all

function printpathvar () {
    eval value=\"\$$1\"
    echo "$value" | tr ':' '\n'
    unset value
}

function editpathvar () {
    local tmp=`mktemp`
    echo "Outputting to $tmp..."
    printpathvar $1 > $tmp
    $EDITOR $tmp
    export $1=`cat $tmp | tr '\n' ':'`
    rm $tmp
}

function path_rm () {
    export $1=$(printpathvar $1 | /bin/grep -v "$2" | tr '\n' ':')
}


These functions I swiped from some thread somewhere. I don't really use path_add, but I use path_prepend VAR path and path_append VAR path all the time.
Spoiler:

Code: Select all

# these three functions from http://www.prolangs.rutgers.edu/~dufour/linux/files/zshrc
function path_add () {
    eval value=\"\$$2\"
    case "$value" in
        *:$3:*|*:$3|$3:*|$3)
            result="$value"
            ;;
        "")
            export $2
            result="$3"
            ;;
        *)
            case "$1" in
                p*)
                    result="$3:${value}"
                    ;;
                *)
                    result="${value}:$3"
                    ;;
            esac
    esac
    eval $2=$result
    unset result value
}

# convenience routine which appends a string to a path.
function path_append () {
    path_add append "$1" "$2"
}

# convenience routine which prepends a string to a path.
function path_prepend () {
    path_add prepend "$1" "$2"
}


Just a convenience (ff = "find file"):

Code: Select all

function ff () {
    find . -iname "*$1*"
}

function mkcd() {
  mkdir $1
  cd $1
}


I like setting AUTO_PUSHD and PUSHD_SILENT (setopt AUTO_PUSHD, setopt PUSHD_SILENT; these may be zsh-only). The former turns every cd into a pushd, and the latter tells pushd to not print the directory stack. The downside is that you get some "extraneous" directories in there that you wouldn't have pushded if you were making a distinction; I make up for that with the following function popd_until string which popds until it gets to a directory containing the string.
Spoiler:

Code: Select all

function popd-until () {
    local TARGET="$1"
    if [[ $(dirs) = *$TARGET* ]]; then
        while [[ $PWD != *$TARGET* ]]; do
            if popd; then
                ;
            else
                echo "Uh oh!"
                exit 1
            fi
        done
        echo $PWD
    else
        echo "Current directory stack does not contain $TARGET"
    fi
}


For zsh users, setopt INC_APPEND_HISTORY; adds lines to the history file as they are run instead of replacing the history file (default) or waiting until the shell exits (APPEND_HISTORY). Also SAVEHIST=9999 and HISTSIZE=9999. And setopt EXTENDED_HISTORY which records start time and how long each command runs for, according to the explanation blurb. (I actually didn't realize it records the latter... I have my own complicated set of pre-command and prompt hooks to try to get the length of time a command took. I'll have to see if I can replace that.)

setopt NO_CASE_GLOB because I'm ideologically opposed to case-sensitive file systems. :-)

setopt IGNORE_EOF because I'm klutzy.


And in case any one is really interested, here are my aliases (probably many of these will scare a lot of old Unix fogies :-p):
Spoiler:

Code: Select all

alias ls="/bin/ls --color=always --sort=version -B -h"
alias ll="/bin/ls --color=always --sort=version --si -lAh"
alias l="ls -Bd"
alias cp="cp --preserve"

alias grep="grep --color=always"
alias h="history | grep"

alias dirs="dirs -v"

alias open="gnome-open"

alias less="less -R"

# htop is an awesome program and better than top in every way :-)
alias top="htop"

alias cvs="cvs -q"

alias psg="ps aux | grep"

# I don't really use these... maybe I should
alias tarc="tar cvf"
alias tarcj="tar cvjf"
alias tarcz="tar cvzf"

alias pu="popd-until"

# Removes any color from output (useful because of my propensity to force colors on)
alias decolor='perl -pe "s/\033[^m]*m//g;"'

alias clean-emacs="rm *~ #*#"

# dwdiff is an awesome awesome program
alias dwdiff="dwdiff -c"

alias numeric-total="awk '{tot=tot+\$1} END {print tot}'"

# Put the sha1 hash of the working copy of a git repository on the clipboard.
# Actually since there are a bunch of clipboards, use the shotgun approach
# and put them on several of them.
function githead() {
    git rev-parse HEAD | tr -d "\n" | xsel -p
    git rev-parse HEAD | tr -d "\n" | xsel -s
    git rev-parse HEAD | tr -d "\n" | xsel -b
}

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Sat Aug 06, 2011 9:11 pm UTC

EvanED wrote:My other "trick" that will change the way you use your shell if you don't know about it is its backwards incremental search -- access it by ctrl-R and start typing.

Yep, that one I do know about. I don't have much use for disown, though, since I'm usually running either screen or tmux.

userxp
Posts: 436
Joined: Thu Jul 09, 2009 12:40 pm UTC

Re: Coding: Fleeting Thoughts

Postby userxp » Tue Aug 09, 2011 11:37 pm UTC

If I were creating a new programming language, I'd make it have built-in HTML output for quick and easy graphical interfacing. Not sure how easy that would be to implement (no Javascript of course, otherwise combining the two languages would be a mess).

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Wed Aug 10, 2011 12:07 am UTC

userxp wrote:If I were creating a new programming language, I'd make it have built-in HTML output for quick and easy graphical interfacing. Not sure how easy that would be to implement (no Javascript of course, otherwise combining the two languages would be a mess).

Something analogous to LINQ's syntax?


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 11 guests